<commit_before>
"""
Zabbix Handler

Provides a class responsible for the communication with Zabbix, including access to several API methods
"""

__authors__ = "<NAME>, <NAME>, <NAME>"
__copyright__ = "Copyright (c) 2014 OneSource Consultoria Informatica, Lda"
__license__ = "Apache 2"
__contact__ = "www.onesource.pt"
__date__ = "01/09/2014"

__version__ = "1.0"

import urllib2
import json
import time

class ZabbixHandler:
    def __init__(self, keystone_admin_port, compute_port, admin_user, zabbix_admin_pass, zabbix_host, keystone_host,
                 template_name, zabbix_proxy_name, keystone_auth):

        self.keystone_admin_port = keystone_admin_port
        self.compute_port = compute_port
        self.zabbix_admin_user = admin_user
        self.zabbix_admin_pass = zabbix_admin_pass
        self.zabbix_host = zabbix_host
        self.keystone_host = keystone_host
        self.template_name = template_name
        self.zabbix_proxy_name = zabbix_proxy_name
        self.keystone_auth = keystone_auth
        #self.token = keystone_auth.getToken()
        full_token= keystone_auth.getTokenV2()
        self.token = full_token['id']
        self.token_expires = full_token['expires']
        
        self.logger=keystone_auth.logger
        self.logger.info("Zabbix handler initialized")


    def first_run(self):

        self.api_auth = self.get_zabbix_auth()
        self.proxy_id = self.get_proxy_id()
        self.template_id = self.get_template_id()
        tenants = self.get_tenants()
        self.group_list = []
        self.group_list = self.host_group_list(tenants)
        self.check_host_groups()
        self.check_instances()

        self.logger.info("Zabbix first run performed")

    def get_zabbix_auth(self):
        """
        Method used to request a session ID form Zabbix API by sending Admin credentials (user, password)

        :return: returns an Id to use with zabbix api calls
        """
        payload = {"jsonrpc": "2.0",
                   "method": "user.login",
                   "params": {"user": self.zabbix_admin_user,
                              "password": <PASSWORD>},
                   "id": 2}
        response = self.contact_zabbix_server(payload)
        zabbix_auth = response['result']
        return zabbix_auth

    def create_template(self, group_id):
        """
        Method used to create a template.

        :param group_id: Receives the template group id
        :return:   returns the template id
        """
        print "Creating Template and items"
        payload = {"jsonrpc": "2.0",
                   "method": "template.create",
                   "params": {
                       "host": self.template_name,
                       "groups": {
                           "groupid": group_id
                       }
                   },
                   "auth": self.api_auth,
                   "id": 1
        }
        response = self.contact_zabbix_server(payload)
        template_id = response['result']['templateids'][0]
        self.create_items(template_id)
        return template_id

    def create_items(self, template_id):
        """
        Method used to create the items for measurements regarding the template
        :param template_id: receives the template id
        """
        items_list = ['cpu', 'cpu_util', 'disk.read.bytes', 'disk.write.bytes',
                      'disk.write.requests',
                      'disk.read.requests', 'network.incoming.bytes', 'network.incoming.packets',
                      'network.outgoing.bytes',
                      'network.outgoing.packets']
        for item in items_list:
            if item == 'cpu':
                value_type = 3
            else:
                value_type = 0
            payload = self.define_item(template_id, item, value_type)
            self.contact_zabbix_server(payload)

    def define_item(self, template_id, item, value_type):
        """
        Method used to define the items parameters

        :param template_id:
        :param item:
        :param value_type:
        :return: returns the json message to send to zabbix API
        """

        payload = {"jsonrpc": "2.0",
                   "method": "item.create",<commit_msg>initial refinements to data type<commit_after>
"""
Zabbix Handler

Provides a class responsible for the communication with Zabbix, including access to several API methods
"""

__authors__ = "<NAME>, <NAME>, <NAME>"
__copyright__ = "Copyright (c) 2014 OneSource Consultoria Informatica, Lda"
__license__ = "Apache 2"
__contact__ = "www.onesource.pt"
__date__ = "01/09/2014"

__version__ = "1.0"

import urllib2
import json
import time

class ZabbixHandler:
    def __init__(self, keystone_admin_port, compute_port, admin_user, zabbix_admin_pass, zabbix_host, keystone_host,
                 template_name, zabbix_proxy_name, keystone_auth):

        self.keystone_admin_port = keystone_admin_port
        self.compute_port = compute_port
        self.zabbix_admin_user = admin_user
        self.zabbix_admin_pass = zabbix_admin_pass
        self.zabbix_host = zabbix_host
        self.keystone_host = keystone_host
        self.template_name = template_name
        self.zabbix_proxy_name = zabbix_proxy_name
        self.keystone_auth = keystone_auth
        #self.token = keystone_auth.getToken()
        full_token= keystone_auth.getTokenV2()
        self.token = full_token['id']
        self.token_expires = full_token['expires']
        
        self.logger=keystone_auth.logger
        self.logger.info("Zabbix handler initialized")


    def first_run(self):

        self.api_auth = self.get_zabbix_auth()
        self.proxy_id = self.get_proxy_id()
        self.template_id = self.get_template_id()
        tenants = self.get_tenants()
        self.group_list = []
        self.group_list = self.host_group_list(tenants)
        self.check_host_groups()
        self.check_instances()

        self.logger.info("Zabbix first run performed")

    def get_zabbix_auth(self):
        """
        Method used to request a session ID form Zabbix API by sending Admin credentials (user, password)

        :return: returns an Id to use with zabbix api calls
        """
        payload = {"jsonrpc": "2.0",
                   "method": "user.login",
                   "params": {"user": self.zabbix_admin_user,
                              "password": <PASSWORD>},
                   "id": 2}
        response = self.contact_zabbix_server(payload)
        zabbix_auth = response['result']
        return zabbix_auth

    def create_template(self, group_id):
        """
        Method used to create a template.

        :param group_id: Receives the template group id
        :return:   returns the template id
        """
        print "Creating Template and items"
        payload = {"jsonrpc": "2.0",
                   "method": "template.create",
                   "params": {
                       "host": self.template_name,
                       "groups": {
                           "groupid": group_id
                       }
                   },
                   "auth": self.api_auth,
                   "id": 1
        }
        response = self.contact_zabbix_server(payload)
        template_id = response['result']['templateids'][0]
        self.create_items(template_id)
        return template_id

    def create_items_legacy(self, template_id):
        """
        Method used to create the items for measurements regarding the template
        :param template_id: receives the template id
        """
        items_list = ['cpu', 'cpu_util', 'disk.read.bytes', 'disk.write.bytes',
                      'disk.write.requests',
                      'disk.read.requests', 'network.incoming.bytes', 'network.incoming.packets',
                      'network.outgoing.bytes',
                      'network.outgoing.packets']
        for item in items_list:
            if item == 'cpu':
                value_type = 3
            else:
                value_type = 0
            payload = self.define_item(template_id, item, value_type)
            self.contact_zabbix_server(payload)

    def create_items(self, template_id):
        """
        Method used to create the items for measurements regarding the template
        :param template_id: receives the template id
        """
        items_list = ['cpu', 'cpu_util', 'disk.read.bytes', 'disk.write.bytes',
                      'disk.write.requests',
                      'disk.read.requests', 'network.incoming.bytes', 'network.incoming.packets',
                      'network.outgoing.bytes',
                      'network.outgoing.packets']

        ####  defaults
        value_type=0
        delta=0
        units=""
        multiplier="1"
        #########

        for item in items_list:
            if item == 'cpu':
                value_type = 3
                units='ns'
                delta=2

            if item == "cpu_util":
                units="%"

            if item.startswith('network.'):
                delta=1 #speed
                if item.endswith('bytes'):
                    value_type=3 #numeric unsigned
                    units='Kbps'
                    multiplier=1024

            if item.startswith('disk.'):
                delta=2
                if item.endswith('bytes'):
                    units='MB'
                    multiplier=1048576
            
        
            payload = self.define_item(template_id, item, value_type,delta,units,multiplier)
            self.contact_zabbix_server(payload)
 

    def define_item(self, template_id, item, value_type,delta,units,multiplier):
        """
        Method used to define the items parameters

        :param template_id:
        :param item:
        :param value_type:
        :return: returns the json message to send to zabbix API
        """

        payload = {"jsonrpc": "2.0",
                   "method": "item.create",
                   "params": {
                       "name": item,
                       "key_": item,
                       "hostid": template_id,
                       "type": 2,
                       "value_type": value_type,
                       "history": "90",
                       "trends": "365",
                       "units": units,
                       "delta": delta,
                       "formula": multiplier,
                       "lifetime": "30",
                       "delay": 10
                   },
                   "auth": self.api_auth,
                   "id": 1}

        return payload

    def define_item_legacy(self, template_id, item, value_type):
        """
        Method used to define the items parameters

        :param template_id:
        :param item:
        :param value_type:
        :return: returns the json message to send to zabbix API
        """

        payload = {"jsonrpc": "2.0",
                   "method": "item.create",
{'fraction_non_alphanumeric': 0.09678890713373473, 'fraction_numerical': 0.0080277321656632, 'mean_word_length': 1.772635306019221, 'pattern_counts': {'":': 50, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2711246', 'n_tokens_mistral': 3168, 'n_tokens_neox': 2986, 'n_words': 705}