Beispiel #1
0
	def gen_calc_item_json(self,item_name,host_id,formula,host_ip=None,interface_id=None,item_type=15,value_type=0):
		session = loadSession()
		i = session.query(Zabbixinterface).filter_by(hostid=host_id).first()
		interface_id = i.interfaceid
		host_ip = i.ip
		session.close()
		data = json.dumps({
			"jsonrpc":"2.0",
			"method":"item.create",
			"params":{
				"name":item_name,
				"key_":item_name,
				"hostid": str(host_id),
				"type":item_type,
				"value_type":value_type,
				"interfaceid":interface_id,
				"trapper_hosts":host_ip,
				"params": formula,
				"delay":60
			},
			"auth":self.user_login(),
			"id":1
		})

		return data
Beispiel #2
0
def add_itkey_to_host(hostid, it, zabbix):
    host = Host.query.filter_by(hostid=hostid).first()
    if host == None:
        raise HostNotExist('host do not exists')

    if it == None:
        raise MonitorException('it to be add do not exists')

    session = loadSession()
    zitem = session.query(Zabbixitems).filter_by(
        hostid=hostid, key_=it.uniqueindexname).first()
    session.close()

    itemid = None

    if zitem != None:
        itemid = zitem.itemid
    else:
        itemid = zabbix.item_create(it.itemkey, hostid, None, None, 2,
                                    it.zabbixvaluetype)

    item = host.items.filter_by(itemid=itemid).first()
    if item != None:
        item.itemname = it.itemtypename
    else:
        item = Item(itemid, it.itemtypename, host, it)

    db.session.add(item)
Beispiel #3
0
def delete_template_item(it, zabbix):

    session = loadSession()
    delete_itemids = []
    for s in it.service.all():
        group_template = session.query(Zabbixhosts).filter_by(
            host=ZABBIX_TEMPLATE_PREFIX + TEMPLATE_GROUP_SPLITER +
            s.servicename).first()
        if group_template == None:
            raise Exception('it group do not exists')
        item = session.query(Zabbixitems).filter_by(
            name=it.itemkey, hostid=group_template.hostid).first()
        if item != None:
            delete_itemids.append(item.itemid)

    if it.nit != None:
        normal_template = session.query(Zabbixhosts).filter_by(
            host=NORMAL_TEMPLATE_NAME).first()
        if normal_template == None:
            raise Exception('it normal group do not exists')
        item = session.query(Zabbixitems).filter_by(
            name=it.itemkey, hostid=normal_template.hostid).first()
        if item != None:
            delete_itemids.append(item.itemid)

    session.close()
    zabbix.item_delete(delete_itemids)
Beispiel #4
0
	def template_delete_without_clear(self,templateids):

		delete_tids = []

		session = loadSession()
		for t in templateids:
			if session.query(Zabbixhosts).filter_by(hostid=t).count():
				tmp = {'templateid' : t}
				delete_tids.append(tmp)

		session.close()

		if len(delete_tids) == 0:
			return None

		data = json.dumps({
			"jsonrpc": "2.0",
    		"method": "template.massupdate",
    		"params": 
    		{
    			'templates': delete_tids,
    			'hosts': []
    		},
    		"auth": self.user_login(),
    		"id": 1
		})

		api_result = self.request_api(data)
		return api_result
Beispiel #5
0
	def get_formula_for_items(cls,itemids):
		result = ''
		count = 0
		session = loadSession()
		try:
			for itemid in itemids:
				item_formula = ''
				if count == 0:
					count += 1
				else:
					item_formula = '+'

				item_formula += 'last("'
				
				zi = session.query(Zabbixitems).filter_by(itemid=itemid).first()
				hostid = zi.hostid
				hostname = session.query(Zabbixhosts).filter_by(hostid=hostid).first().name
				
				item_formula += hostname
				item_formula += ':'
				item_formula += zi.key_
				item_formula += '")'

				result += item_formula
			session.close()
		except Exception, e:
			raise Exception('get formula for item error, ' + str(e))
Beispiel #6
0
def init_service():

    # if len(names) == 0:
    # 	names = ['nat','web','relay','control','database','monitor']

    zabbix = zabbix_api()
    session = loadSession()
    aws_host_group = session.query(Zabbixhostgroup).filter_by(
        name=HOST_GROUP_NAME).first()
    if aws_host_group == None:
        raise Exception('host group :' + HOST_GROUP_NAME + 'do not exist')

    # servicenames = []
    templates = session.query(Zabbixhosts).filter(
        Zabbixhosts.name.ilike('%' + TEMPLATE_GROUP_SPLITER + '%')).all()

    # group = {'groupid':aws_host_group.groupid}
    try:
        for t in templates:
            tname = t.name
            servicename = t.name.split(TEMPLATE_GROUP_SPLITER)[1]
            s = Service.query.filter_by(servicename=servicename).first()
            if s == None:
                tmp = Service(servicename=servicename)
                db.session.add(tmp)

        db.session.commit()
        session.close()
    except Exception, e:
        db.session.rollback()
        zabbix.rollback()
        traceback.print_exc(file=sys.stdout)
        raise e
Beispiel #7
0
	def get_hostinfo_list_for_item(cls,itemids):
		tmp_result = []
		session = loadSession()
		try:
			for itemid in itemids:

				item = Item.query.get(itemid)

				if item is None:
					continue

				host = item.host

				tmp = host.hostname

				zi = session.query(Zabbixitems).filter_by(itemid=itemid).first()
				hostid = zi.hostid
				hostip = session.query(Zabbixhosts).filter_by(hostid=hostid).first().name

				tmp += '(' + hostip + ')'

				tmp_result.append(tmp)
				
			session.close()
		except Exception, e:
			raise Exception('get hostinfo for item error, ' + str(e))
Beispiel #8
0
def get_host_interfaceid(hostid):
    session = loadSession()
    i = session.query(Zabbixinterface).filter_by(hostid=hostid).first()
    session.close()
    if i == None:
        raise IpAddressNotExist('ip address do not exists')
    interface_id = i.interfaceid
    return interface_id
Beispiel #9
0
def delete_group_template(servicename, zabbix):

    session = loadSession()
    group_template = session.query(Zabbixhosts).filter_by(
        host=ZABBIX_TEMPLATE_PREFIX + TEMPLATE_GROUP_SPLITER +
        servicename).first()
    if group_template != None:
        zabbix.template_delete([group_template.hostid])
    session.close()
Beispiel #10
0
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
Beispiel #11
0
    def server_2_server_data(self, input_dict):


        time_from = input_dict['time_from']
        time_to = input_dict['time_to']

        data = input_dict['data']


        row_content_generator = RowContentGeneratorFactory()\
            .produce_generator(BY_GROUP_RESULT)

        assert row_content_generator != None, 'undefined row generator'

        session = loadSession()
        result = {}
        result['data'] = []
        for d in data:
            from_tag = d['FROM']
            to_tag = d['TO']
            servicename = d['servicename']
            metricname = d['metricname']
            ground = 60
            sm = servicename

            itemids = row_content_generator.content_2_id(\
                row_content_generator.get_fake_row(sm, metricname))

            if len(itemids) > 0:

                history_data = Chart.item_list_2_history_data(itemids,\
                    ground, time_from,time_to)
                series_data = Chart.\
                    item_history_data_2_chart_update_series_data(history_data,\
                    time_from, 1)
                last_sum = series_data[1]

                tmp = {
                    'FROM' : from_tag,
                    'TO' : to_tag,
                    'servicename': servicename,
                    'metricname': metricname,
                    'value': last_sum,
                    'alias':d['alias']
                }

                result['data'].append(tmp)



        result['time_from'] = input_dict['time_from']
        result['time_to'] = input_dict['time_to']
        session.close()

        return result
Beispiel #12
0
    def business_health(self,input_dict):
        services_metrics = input_dict['service_metrics']

        time_from = input_dict['time_from']
        time_to = input_dict['time_to']

        session = loadSession()
        result = {}

        row_content_generator = RowContentGeneratorFactory()\
            .produce_generator(BY_GROUP_RESULT)

        assert row_content_generator != None, 'undefined row generator'

        for sm in services_metrics:
            group = Service.query.filter_by(servicename=sm).first()
            if group != None:
                result[sm] = []
                for metric_condition in services_metrics[sm]:
                    metricname = metric_condition['metricname']
                    condition = metric_condition['condition']
                    itemids = row_content_generator.content_2_id(\
                        row_content_generator.get_fake_row(sm, metricname))

                    if len(itemids) > 0:
                        recordsuint = Zabbixhistoryuint.\
                            get_interval_condition_record(itemids,\
                            time_from, time_to,condition)
                        records = Zabbixhistory.\
                            get_interval_condition_record(itemids,\
                            time_from, time_to, condition)

                        if records is None and recordsuint is None:
                            continue

                        tmp_records = recordsuint if recordsuint is not None \
                            else records

                        for r in tmp_records:
                            itemid = r[0]
                            clock = r[1]
                            value = r[2]
                            item = Item.query.get(itemid)
                            if item != None:
                                host = item.host
                                zinterface = session.query(Zabbixinterface).\
                                    filter_by(hostid=host.hostid).first()
                                if zinterface != None:
                                    result[sm].append([host.hostname, \
                                    zinterface.ip, item.itemname, clock, value])


        session.close()
        return result
Beispiel #13
0
def add_group_template(servicename, zabbix):

    session = loadSession()
    group = session.query(Zabbixhostgroup).filter_by(
        name=HOST_GROUP_NAME).first()
    if group == None:
        raise Exception('default group do not exists')
    group_dict = {"groupid": group.groupid}
    zabbix.template_create(
        ZABBIX_TEMPLATE_PREFIX + TEMPLATE_GROUP_SPLITER + servicename,
        group_dict)
    session.close()
Beispiel #14
0
    def core_data(self,input_dict):
        services_metrics = input_dict
        session = loadSession()
        result = {}

        row_content_generator = RowContentGeneratorFactory()\
            .produce_generator(BY_GROUP_RESULT)

        assert row_content_generator != None, 'undefined row generator'

        for sm in services_metrics:
            group = Service.query.filter_by(servicename=sm).first()
            if group != None:
                result[sm] = {}
                for metric in services_metrics[sm]:
                    time_from = services_metrics[sm][metric]['time_from']
                    time_to = services_metrics[sm][metric]['time_to']
                    ground = int(services_metrics[sm][metric]['frequency'])

                    result[sm][metric] = {}

                    itemids = row_content_generator.content_2_id(\
                        row_content_generator.get_fake_row(sm, metric))

                    # itemids = ItemSearch.find_item_list_for_table_row_group([sm,metric])

                    if len(itemids) > 0 :
                        v = Zabbixhistory.get_interval_history_no_ground(\
                            itemids, time_from, time_to)
                        vuint = Zabbixhistoryuint.\
                            get_interval_history_no_ground(itemids, \
                            time_from, time_to)

                        if vuint is  None and v is None:
                            continue

                        result[sm][metric]['statics'] = v if v is not None \
                            else vuint

                        time_from = time_to - ground
                        time_from = (time_from//ground) * ground

                        history_data = Chart.item_list_2_history_data(itemids, \
                            ground, time_from, time_to)
                        series_data = Chart.\
                            item_history_data_2_chart_update_series_data(\
                            history_data, time_from, 1)
                        last_sum = series_data[1]

                        result[sm][metric]['last'] = last_sum

        session.close()
        return result
Beispiel #15
0
	def host_update(self,hostid,hostname=None,host_ip=None,added_template_name=None):
		template_list=[]
		session = loadSession()
		i = session.query(Zabbixinterface).filter_by(hostid=hostid).first()
		h = session.query(Zabbixhosts).filter_by(hostid=hostid).first()
		tmp_hostname = h.name
		tmp_host_ip = i.ip
		template_name = ['Template OS Linux']
		if added_template_name is not None:
			# template_name.append(added_template_name)
			template_name = list( set(template_name) | set(added_template_name) )
		for template in template_name:
			if session.query(Zabbixhosts).filter_by(name = template).count() > 0:
				var = {}
				t = session.query(Zabbixhosts).filter_by(name = template).first()
				var['templateid'] = t.hostid
				template_list.append(var)
			else:
				raise MonitorException('Template not found ' + template)
		session.close()
		if hostname == None:
			hostname = tmp_hostname

		if host_ip == None:
			host_ip = tmp_host_ip

		data = json.dumps({ 
			"jsonrpc":"2.0", \
			"method":"host.update", 
			"params":{
				"hostid": hostid ,
				"host": hostname,
				"name":hostname,
				"templates": template_list,
			}, 
			"auth": self.user_login(), 
			"id":1
		})
		request = urllib2.Request(self.url, data)
		for key in self.header:
			request.add_header(key, self.header[key]) 
		try:
			result = urllib2.urlopen(request)
		except URLError as e:
			raise MonitorException('cannot update host')
		else:
			response = json.loads(result.read())
			result.close()
			if response.has_key('result'):
				return response['result']['hostids'][0]
			else:
				raise MonitorException('cannot update host due to ' + str(response))
Beispiel #16
0
def get_valid_hostid(first, second):
    total = list(set(first) | set(second))
    result = None
    session = loadSession()
    for itemid in total:

        item = session.query(Zabbixitems).filter_by(itemid=itemid).first()
        if item != None:
            result = session.query(Zabbixhosts).filter_by(
                hostid=item.hostid).first()
            break
    session.close()
    return result
Beispiel #17
0
	def gen_host_create_json(self,host_name , host_ip , hostgroup_name, template_name):
 		if len(hostgroup_name) == 0:
 			raise MonitorException('host group cannot be null')

		group_list=[]
		template_list=[]
		session = loadSession()

		for hostgroup in hostgroup_name:
			if session.query(Zabbixhostgroup).filter_by(name = hostgroup).count() > 0:
				var = {}
				var['groupid'] = session.query(Zabbixhostgroup).filter_by(name = hostgroup).first().groupid
				group_list.append(var)
				# print "yes"
			else:
				raise MonitorException('Group not found ' + hostgroup)

		for template in template_name:
			if session.query(Zabbixhosts).filter_by(name = template).count() > 0:
				var = {}
				t = Zabbixhosts.query.filter_by(name = template).first()
				var['templateid'] = t.hostid
				template_list.append(var)
			else:
				raise MonitorException('Template not found ' + template)
		
		session.close()
		data = json.dumps({ 
			"jsonrpc":"2.0",
			"method":"host.create",
			"params":{
				"host": host_name,
				"interfaces": [
					{
						"type": 1,
						"main": 1,
						"useip": 1,
						"ip": host_ip,
						"dns": "",
						"port": "10050"
					}
				],
				"groups": group_list,
				"templates": template_list
			},
			"auth": self.user_login(),
			"id":1
		})

		return data 
Beispiel #18
0
	def get_host_ip(self):

		assert self.host is not None, 'current item: %s does not ' + \
			'belong to any host' % (self)

		assert self.host.hostid is not None

		ip = 'unkown'
		session = loadSession()
		zi = session.query(Zabbixinterface).\
			filter_by(hostid=self.host.hostid).first()
		session.close()
		if zi != None:
			ip = zi.ip

		return ip
Beispiel #19
0
def clear_old_template(zabbix):

	session = loadSession()
	services_template = session.query(Zabbixhosts).filter(Zabbixhosts.name.ilike('%' + TEMPLATE_GROUP_SPLITER + '%')).all()
	tids = []
	for st in services_template:
	    tids.append(st.hostid)

	n = session.query(Zabbixhosts).filter_by(host=NORMAL_TEMPLATE_NAME).first()
	if n != None:
		ntid = n.hostid
		tids.append(ntid)

	session.close()
	zabbix.template_delete_without_clear(tids)
	zabbix.template_delete(tids)
Beispiel #20
0
    def mechine_health(self,input_dict):
        services = input_dict['services']

        time_from = input_dict['time_from']
        time_to = input_dict['time_to']

        metrics = input_dict['metrics']
        session = loadSession()
        result = {}

        row_content_generator = RowContentGeneratorFactory()\
            .produce_generator(PER_INSTANCE_RESULT)

        assert row_content_generator != None, 'undefined row generator'

        for s in services:
            group = Service.query.filter_by(servicename=s).first()
            if group != None:
                result[s] = {}
                for h in group.hosts.all():
                    zinterface = session.query(Zabbixinterface).\
                        filter_by(hostid=h.hostid).first()
                    zh = session.query(Zabbixhosts).get(h.hostid)
                    if zinterface is None or zh is None:
                        continue
                    result[s][zinterface.ip] = {}
                    result[s][zinterface.ip]['available'] = zh.available

                    for m in metrics:
                        itemids = row_content_generator.content_2_id(\
                            row_content_generator.get_fake_row(zinterface.ip, \
                            m['metricname']))
                        # itemids = ItemSearch.find_item_list_for_table_row_instance([None,None,zinterface.ip,m['metricname'],None, None, None])

                        if len(itemids) > 0:
                            v = Zabbixhistory.get_interval_history_no_ground(\
                                    itemids,time_from,time_to)
                            vuint = Zabbixhistoryuint.\
                                get_interval_history_no_ground(\
                                itemids,time_from,time_to)
                            if vuint is  None and v is None:
                                continue
                            result[s][zinterface.ip][m['metricname']] = \
                                v if v is not None else vuint

        session.close()
        return result
Beispiel #21
0
	def get_available_status(self):

		assert self.host is not None, 'current item: %s does not ' + \
			'belong to any host' % (self)

		assert self.host.hostid is not None

		available_status = None

		session = loadSession()
		zh = session.query(Zabbixhosts).get(self.host.hostid)
		session.close()
		
		if zh != None:
			available_status = zh.available

		return available_status
Beispiel #22
0
	def gen_normal_item_json(self,item_name,host_id,host_ip=None,interface_id=None,item_type=2,value_type=0,unitname=None):
		session = loadSession()
		i = session.query(Zabbixinterface).filter_by(hostid=host_id).first()
		if i != None:
			interface_id = i.interfaceid
		h = session.query(Zabbixhosts).filter_by(hostid=host_id).first()
		# print 'generate normal ' , h
		hostname = h.name
		session.close()
		data = None
		if unitname == None:
			data = json.dumps({
				"jsonrpc":"2.0",
				"method":"item.create",
				"params":{
					"name":item_name,
					"key_":item_name,
					"hostid": str(host_id),
					"type":item_type,
					"value_type":value_type
				},
				"auth":self.user_login(),
				"id":1
			})
		else:
			data = json.dumps({
				"jsonrpc":"2.0",
				"method":"item.create",
				"params":{
					"name":item_name,
					"key_":item_name,
					"hostid": str(host_id),
					"type":item_type,
					"value_type":value_type,
					"units": unitname
				},
				"auth":self.user_login(),
				"id":1
			})

		# if interfaceid != None:
			


		return data
Beispiel #23
0
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
Beispiel #24
0
	def host_delete(self,hostids):
		delete_hostids = []
		session = loadSession()
		for i in hostids:
			host = session.query(Zabbixhosts).filter_by(hostid=i).first()
			if host != None:
				delete_hostids.append(i)
		session.close()
		if len(delete_hostids) == 0:
			return None
		data = json.dumps({
					"jsonrpc":"2.0",
					"method":"host.delete",
					"params":delete_hostids,
					"auth":self.user_login(),
					"id":1
		})

		api_result = self.request_api(data)
		return api_result
Beispiel #25
0
	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
Beispiel #26
0
	def hostgroup_delete(self,groupids):
		delete_hgs = []
		session = loadSession()
		for g in groupids:
			if session.query(Zabbixhostgroup).filter_by(groupid=g).count() > 0:
				delete_hgs.append(g)

		session.close()

		if len(delete_hgs) == 0:
			return None

		data = json.dumps({
			"jsonrpc":"2.0",
			"method":"hostgroup.delete",
			"params":delete_hgs,
			"auth":self.user_login(),
			"id":1
		})
		api_result = self.request_api(data)
		return api_result
Beispiel #27
0
	def action_delete(self,actionids):
		delete_actionids = []
		session = loadSession()
		for a in actionids:
			if session.query(Zabbixactions).filter_by(actionid=a).count() > 0:
				delete_actionids.append(a)

		session.close()
		if len(delete_actionids) == 0:
			return None

		data = json.dumps({
					"jsonrpc":"2.0",
					"method":"action.delete",
					"params":delete_actionids,
					"auth":self.user_login(),
					"id":1
		})

		api_result = self.request_api(data)
		return api_result
Beispiel #28
0
	def trigger_delete(self,triggerids):
		delete_triggerids = []
		session = loadSession()
		for t in triggerids:
			if session.query(Zabbixtriggers).filter_by(triggerid=t).count() > 0:
				delete_triggerids.append(t)

		session.close()
		if len(delete_triggerids) == 0:
			return None

		data = json.dumps({
					"jsonrpc":"2.0",
					"method":"trigger.delete",
					"params":delete_triggerids,
					"auth":self.user_login(),
					"id":1
		})

		api_result = self.request_api(data)
		return api_result
Beispiel #29
0
	def item_delete(self,itemids):
		# precheck if the item exist in zabbix
		session = loadSession()
		delete_itemids = []
		for itemid in itemids:
			if session.query(Zabbixitems).filter_by(itemid=itemid).count() > 0:
				delete_itemids.append(itemid)

		session.close()
		if len(delete_itemids) == 0:
			return None
		# di = [itemid]
		data = json.dumps({
					"jsonrpc":"2.0",
					"method":"item.delete",
					"params":delete_itemids,
					"auth":self.user_login(),
					"id":1
		})
		api_result = self.request_api(data)
		return api_result
Beispiel #30
0
	def drule_delete(self,druleids):
		session = loadSession()
		delete_drules = []
		for d in druleids:
			if session.query(Zabbixdrules).filter_by(druleid=d).count() > 0:
				delete_drules.append(d)
		session.close()
		if len(delete_drules) == 0:
			return None

		data = json.dumps(
		{
			"jsonrpc": "2.0",
    		"method": "drule.delete",
    		"params": delete_drules,
    		"auth": self.user_login(),
    		"id": 1
		})

		api_result = self.request_api(data)

		return api_result