def init_aws_item(): # add aws item in current host host_name = get_zabbix_server_ip() zabbix = zabbix_api() init_aws() # checkout if current host exists in zabbix database session = loadSession() tmphost = session.query(Zabbixhosts).filter_by(name=host_name).first() session.close() hostid = None try: # will create in zabbix if do not exist if tmphost != None: hostid = tmphost.hostid if hostid == None: pass #host_group_name = ['AWS servers'] #template_name = ['Template OS Linux'] #zabbix.host_create(host_name,host_name,host_group_name,template_name) print "hostid", hostid # get all regions for aws rs = boto.ec2.cloudwatch.regions() # get data type category and will create if do not exist idt = Itemdatatype.query.filter_by( itemdatatypename='AWS fee data').first() print "idt", idt if idt == None: idt = Itemdatatype(itemdatatypename='AWS fee data') db.session.add(idt) # get aws data for every area for r in rs: a = Area.query.filter_by(areaname=r.name).first() print r.name if a == None: a = Area(areaname=r.name) db.session.add(a) con = boto.ec2.cloudwatch.connect_to_region(r.name) lms = con.list_metrics(None, None, metric_name="EstimatedCharges", namespace="AWS/Billing") for lm in lms: init_aws_itemtype(lm.dimensions, idt, hostid, a, zabbix) db.session.commit() except BotoServerError, e: db.session.commit() pass
def send_update_data(): try: server_ip = get_zabbix_server_ip() host_name = get_zabbix_server_ip() con = boto.ec2.autoscale.connect_to_region(AREA) autoscalegroups = con.get_all_groups() zs = ZabbixSender(server_ip) for asg in autoscalegroups: for key in group_metric_map: if key in asg.name: zs.AddData(host_name, unicode(group_metric_map[key]), str(asg.desired_capacity)) break res = zs.Send() zs.ClearData() except Exception, e: import sys, traceback traceback.print_exc(file=sys.stdout)
def __init__(self): self.created_hosts = [] self.created_items = [] self.created_triggers = [] self.created_actions = [] self.created_hostgroups = [] self.created_discovery_rules = [] self.created_trigger_prototypes = [] self.created_templates = [] server_ip = get_zabbix_server_ip() port = '5000' self.url = 'http://' + server_ip + ':' + port + '/zabbix/api_jsonrpc.php' self.header = {"Content-Type":"application/json"}
def it_test(key): session = loadSession() th = session.query(Zabbixhosts).filter_by( name=get_zabbix_server_ip()).first() session.close() if th == None: raise MonitorException( 'cannot check if the key can be added due to host is not available' ) hostid = th.hostid zabbix = zabbix_api() itemid = zabbix.item_create(key, hostid) try: if itemid != None: zabbix.item_delete([itemid]) except Exception, e: pass
def create_calculate_item_on_monitor_server(cls,zabbix,formula,alarmname): result = {} session = loadSession() host = session.query(Zabbixhosts).filter_by(name=get_zabbix_server_ip()).first() session.close() day = datetime.today() daytime = day.strftime("%Y%m%d") itemname = alarmname + '_' + daytime + construct_random_str() calcitemid = zabbix.calc_item_create(itemname,host.hostid,formula) result['itemid'] = calcitemid result['itemname'] = itemname result['hostname'] = host.name result['hostid'] = host.hostid calc = Calculateditem(calcitemid,formula) db.session.add(calc) result['calculateditem'] = calc return result
def rename_link_add_update(zabbix, host_ip): session = loadSession() server_ip = get_zabbix_server_ip() if host_ip == server_ip or host_ip == '127.0.0.1': exit(0) areaname = AREA zis = session.query(Zabbixinterface).filter_by(ip=host_ip).all() if len(zis) <= 0: exit(0) host_to_be_add = {} for i in zis: hostid = i.hostid if Host.query.get(hostid) is None: zh = session.query(Zabbixhosts).get(hostid) if zh != None: host_to_be_add[hostid] = {} host_to_be_add[hostid]['area'] = areaname host_to_be_add[hostid]['ip'] = i.ip proxy_hostid = zh.proxy_hostid if proxy_hostid != None: zph = session.query(Zabbixhosts).get(proxy_hostid) if zph != None: proxy_name = zph.name host_to_be_add[hostid]['area'] = proxy_name.split( AREA_PROXY_SPLITER)[0] nametag = None servicename = None for hid in host_to_be_add: try: con = boto.ec2.connect_to_region(host_to_be_add[hid]['area']) res = con.get_all_instances( filters={'private_ip_address': host_to_be_add[hid]['ip']}) nametag = res[0].instances[0].tags['Name'] servicename = nametag.split('-')[0] if session.query(Zabbixhosts).filter_by( host=ZABBIX_TEMPLATE_PREFIX + TEMPLATE_GROUP_SPLITER + servicename).count() <= 0: servicename = 'unknown' except Exception, e: nametag = host_to_be_add[hid]['ip'] servicename = 'unknown' tmps = Service.query.filter_by(servicename=servicename).first() if tmps == None: tmps = Service(servicename=servicename) db.session.add(tmps) db.session.commit() host_to_be_add[hid]['nametag'] = nametag host_to_be_add[hid]['servicename'] = servicename added_template_name = [ ZABBIX_TEMPLATE_PREFIX + TEMPLATE_GROUP_SPLITER + servicename, NORMAL_TEMPLATE_NAME ] tmp_hostid = zabbix.host_update( hid, hostname=host_to_be_add[hid]['ip'], host_ip=host_to_be_add[hid]['ip'], added_template_name=added_template_name) add_update_host(host_to_be_add[hid]['nametag'], host_to_be_add[hid]['servicename'], host_to_be_add[hid]['ip'], host_to_be_add[hostid]['area'])
def create_calcitem_trigger_action2(zabbix,formula,functiontype,triggervalue,timeshift,trigger_operator,\ command_path,autoscalegroupname,autoscaletype,areaid): #generate calculate item in zabbix session = loadSession() host = session.query(Zabbixhosts).filter_by( name=get_zabbix_server_ip()).first() session.close() iteminfo = create_calculate_item(zabbix, host.hostid, formula) # save in monitor database calcitem = Calculateditem(iteminfo['itemid'], formula) db.session.add(calcitem) # add trigger and action both in zabbix and monitor hostname = host.name expression = get_trigger_expression(hostname, iteminfo['itemname'], functiontype, triggervalue, timeshift, trigger_operator) name = iteminfo['itemname'] # in zabbix triggerid = zabbix.trigger_create(expression, name) # in monitor trigger = Trigger(triggerid, name, triggervalue, timeshift, calcitem) db.session.add(trigger) # add action in zabbix eventsource = 0 conditions = [{ "conditiontype": 1, "operator": 0, "value": host.hostid }, { "conditiontype": 2, "operator": 0, "value": triggerid }] operations = [{ "operationtype": 1, "opcommand_hst": [{ "hostid": host.hostid }], "opcommand": { "type": 0, "command": command_path, "execute_on": 1 } }] actionid = zabbix.action_create(name, eventsource, conditions, operations) # add action in monitor actionname = get_action_name(command_path) action = Action(actionid, autoscalegroupname, autoscaletype, areaid, command_path, actionname) db.session.add(action) # add relationship between trigger and action ttmp = trigger.add_action(action) if ttmp != None: db.session.add(ttmp)
def create_calcitem_trigger_action(zabbix,formula,functiontype,triggervalue,timeshift,trigger_operator,\ command): session = loadSession() host = session.query(Zabbixhosts).filter_by( name=get_zabbix_server_ip()).first() session.close() iteminfo = create_calculate_item(zabbix, host.hostid, formula) # save in monitor database calcitem = Calculateditem(iteminfo['itemid'], formula) db.session.add(calcitem) # add trigger and action both in zabbix and monitor hostname = host.name expression = get_trigger_expression(hostname, iteminfo['itemname'], functiontype, triggervalue, timeshift, trigger_operator) name = iteminfo['itemname'] result = {} # in zabbix triggerid = zabbix.trigger_create(expression, name) content = "'" + functiontype + ' of ' + formula + ' ' + trigger_operator + ' ' + triggervalue + ' for ' + timeshift + 's' + "'" # in monitor # trigger = Trigger(triggerid,name,triggervalue,timeshift,calcitem) # db.session.add(trigger) # add action in zabbix eventsource = 0 conditions = [{ "conditiontype": 1, "operator": 0, "value": host.hostid }, { "conditiontype": 2, "operator": 0, "value": triggerid }] operations = [{ "operationtype": 1, "opcommand_hst": [{ "hostid": 0 }], "opcommand": { "type": 0, "command": command + ' ' + content, "execute_on": 1 } }] actionid = zabbix.action_create(name, eventsource, conditions, operations) result['trigger_name'] = name result['trigger_id'] = triggerid result['calcitem'] = calcitem result['actionid'] = actionid return result
traceback.print_exc(file=sys.stdout) if __name__ == '__main__': print 'process area' init_area() print 'process service' init_service() print 'process host' zabbix = zabbix_api() try: session = loadSession() h = session.query(Zabbixhosts).filter_by( name=get_zabbix_server_ip()).first() session.close() if h == None: result = zabbix.host_create(get_zabbix_server_ip(), '127.0.0.1', [HOST_GROUP_NAME], [TEMPLATE_NAME]) else: zabbix.host_update(h.hostid) result = h.hostid # area = Area.query.filter_by(areaname=AREA).first() # service = Service.query.filter_by(servicename=SERVICE).first() # h = Host(result,get_zabbix_server_ip(),area,service) add_update_host(get_zabbix_server_ip(), SERVICE, get_zabbix_server_ip(), AREA) print "hostid", result # db.session.add(h)