def update_agent_conf(api_url, username, password, host_name, proxy_name):
    api = ZabbixApi(api_url, username, password)
    api.user_login()
    agent_ipaddress = api.get_interface_ipaddress(host_name, '1')
    proxy_ipaddress = api.get_interface_ipaddress(proxy_name, '1')

    cmd = "/bin/sh %s/action/update_agent_conf.sh %s %s" % (os.path.abspath(os.path.dirname(__file__)), agent_ipaddress, proxy_ipaddress)
    result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return True
 def attach_proxy(self, proxy_name):
     if proxy_name != '':
         api = ZabbixApi(self.api_url, self.username, self.password)
         api.user_login()
         if api.attach_host_proxy(proxy_name, self.host_name):
             print "host: %s attached to proxy: %s" % (self.host_name, proxy_name)
             return True
         else:
             return False
Example #3
0
 def CreateTemplates(self):
     api = ZabbixApi(self.api_url, self.username, self.password)
     api.user_login()
     templates = open(
         "/usr/lib/zabbix/template/zbx_export_templates.xml").read()
     post = json.dumps({
         'jsonrpc': '2.0',
         'method': 'configuration.import',
         'params': {
             'format': 'xml',
             'rules': {
                 'groups': {
                     'createMissing': 'true'
                 },
                 'templates': {
                     'createMissing': 'true',
                     'updateExisting': 'true'
                 },
                 'templateScreens': {
                     'createMissing': 'true',
                     'updateExisting': 'true'
                 },
                 'templateLinkage': {
                     'createMissing': 'true'
                 },
                 'applications': {
                     'createMissing': 'true'
                 },
                 'items': {
                     'createMissing': 'true',
                     'updateExisting': 'true'
                 },
                 'discoveryRules': {
                     'createMissing': 'true',
                     'updateExisting': 'true'
                 },
                 'triggers': {
                     'createMissing': 'true',
                     'updateExisting': 'true'
                 },
                 'graphs': {
                     'createMissing': 'true',
                     'updateExisting': 'true'
                 }
             },
             'source': templates
         },
         'auth': api.auth_token,
         'id': 1
     })
     request = urllib2.Request(self.api_url, post,
                               {"Content-Type": "application/json-rpc"})
     contents = urllib2.urlopen(request)
 def suggest_candidate_proxy(self):
     """
     ToDo
     """
     api = ZabbixApi(self.api_url, self.username, self.password)
     api.user_login()
     proxies = api.get_proxy_list()
     host_count = {}
     for i in range(len(proxies)):
         host_list = api.get_attached_host_list(proxies[i])
         host_count[proxies[i]] = len(host_list)
         print host_list
         print host_count[proxies[i]]
         print host_count
     candidate_proxy = min(host_count, key=(lambda x: host_count[x]))
     print "Proxy: %s" % candidate_proxy
     return candidate_proxy
Example #5
0
 def attach_proxy(self):
     if self.proxy_name != '':
         api = ZabbixApi(self.api_url, self.username, self.password)
         api.user_login()
         api.attach_host_proxy(self.proxy_name, self.host_name)
         print "host: %s attached to proxy: %s" % (self.host_name,
                                                   self.proxy_name)
Example #6
0
 def suggest_candidate_proxy(self):
     """
     ToDo
     """
     api = ZabbixApi(self.api_url, self.username, self.password)
     api.user_login()
     proxies = api.get_proxy_list()
     host_count = {}
     for i in range(len(proxies)):
         host_list = api.get_attached_host_list(proxies[i])
         host_count[proxies[i]] = len(host_list)
         print host_list
         print host_count[proxies[i]]
         print host_count
     candidate_proxy = min(host_count, key=(lambda x: host_count[x]))
     print "Proxy: %s" % candidate_proxy
     return candidate_proxy
Example #7
0
    def CreateAction(self):
        api = ZabbixApi(self.api_url, self.username, self.password)
        api.user_login()

        post = json.dumps({
            'jsonrpc': '2.0',
            'method': 'host.get',
            'params': {
                'output': 'extend',
                'filter': {
                    'host': [self.host_name]
                }
            },
            'auth': api.auth_token,
            'id': 1
        })
        request = urllib2.Request(self.api_url, post,
                                  {"Content-Type": "application/json-rpc"})
        contents = urllib2.urlopen(request)
        contents_dict = json.loads(contents.read())
        host_id = contents_dict["result"][0]["hostid"]

        post = json.dumps({
            'jsonrpc': '2.0',
            'method': 'template.get',
            'params': {
                'output': 'extend',
                'filter': {
                    'host': "Template SSH Agent"
                }
            },
            'auth': api.auth_token,
            'id': 1
        })
        request = urllib2.Request(self.api_url, post,
                                  {"Content-Type": "application/json-rpc"})
        contents = urllib2.urlopen(request)
        contents_dict = json.loads(contents.read())
        template_id = contents_dict["result"][0]["templateid"]

        post = json.dumps({
            'jsonrpc': '2.0',
            'method': 'action.create',
            'params': {
                'name':
                'OCP STARTER Automatic Registration',
                'eventsource':
                2,
                'status':
                0,
                'esc_period':
                0,
                'filter': {
                    'evaltype':
                    0,
                    'conditions': [{
                        'conditiontype': 24,
                        'operator': 2,
                        'value': 'OCP STARTER'
                    }]
                },
                'operations': [{
                    "operationtype": 2
                }, {
                    'operationtype': 6,
                    'optemplate': [{
                        'templateid': template_id
                    }]
                }]
            },
            'auth': api.auth_token,
            'id': 1
        })
        request = urllib2.Request(api.api_url, post,
                                  {"Content-Type": "application/json-rpc"})
        contents = urllib2.urlopen(request)

        post = json.dumps({
            'jsonrpc': '2.0',
            'method': 'action.create',
            'params': {
                'name':
                'Create Proxy Action',
                'eventsource':
                0,
                'evaltype':
                0,
                'status':
                0,
                'esc_period':
                3600,
                'filter': {
                    'evaltype':
                    1,
                    'conditions': [{
                        'conditiontype': 5,
                        'operator': 0,
                        'value': '1'
                    }, {
                        'conditiontype': 3,
                        'operator': 2,
                        'value': 'Create Proxy Triggers'
                    }]
                },
                'operations': [{
                    'operationtype': 1,
                    'esc_step_from': 1,
                    'esc_step_to': 1,
                    'evaltype': 1,
                    'opcommand_hst': [{
                        'hostid': '0'
                    }],
                    'opcommand': {
                        'type':
                        0,
                        'execute_on':
                        1,
                        'command':
                        '/usr/bin/python ' + self.path +
                        '/caller_action.py "CreateProxy" "{HOST.NAME}"'
                    }
                }]
            },
            'auth': api.auth_token,
            'id': 1
        })
        request = urllib2.Request(api.api_url, post,
                                  {"Content-Type": "application/json-rpc"})
        contents = urllib2.urlopen(request)

        post = json.dumps({
            'jsonrpc': '2.0',
            'method': 'action.create',
            'params': {
                'name':
                'Update IPMI interfaces Action',
                'eventsource':
                0,
                'evaltype':
                0,
                'status':
                0,
                'esc_period':
                3600,
                'filter': {
                    'evaltype':
                    1,
                    'conditions': [{
                        'conditiontype': 5,
                        'operator': 0,
                        'value': '1'
                    }, {
                        'conditiontype': 3,
                        'operator': 2,
                        'value': 'SSH Triggers'
                    }]
                },
                'operations': [{
                    'operationtype': 1,
                    'esc_step_from': 1,
                    'esc_step_to': 1,
                    'evaltype': 1,
                    'opcommand_hst': [{
                        'hostid': '0'
                    }],
                    'opcommand': {
                        'type':
                        0,
                        'execute_on':
                        1,
                        'command':
                        '/usr/bin/python ' + self.path +
                        '/caller_action.py "UpdateIpmiInterfaces" "{HOST.NAME}"'
                    }
                }]
            },
            'auth': api.auth_token,
            'id': 1
        })
        request = urllib2.Request(api.api_url, post,
                                  {"Content-Type": "application/json-rpc"})
        contents = urllib2.urlopen(request)

        post = json.dumps({
            'jsonrpc': '2.0',
            'method': 'action.create',
            'params': {
                'name':
                'Update Agent interfaces Action',
                'eventsource':
                0,
                'evaltype':
                0,
                'status':
                0,
                'esc_period':
                3600,
                'filter': {
                    'evaltype':
                    1,
                    'conditions': [{
                        'conditiontype': 5,
                        'operator': 0,
                        'value': '1'
                    }, {
                        'conditiontype': 3,
                        'operator': 2,
                        'value': 'Update Agent interfaces Triggers'
                    }]
                },
                'operations': [{
                    'operationtype': 1,
                    'esc_step_from': 1,
                    'esc_step_to': 1,
                    'evaltype': 1,
                    'opcommand_hst': [{
                        'hostid': '0'
                    }],
                    'opcommand': {
                        'type':
                        0,
                        'execute_on':
                        1,
                        'command':
                        '/usr/bin/python ' + self.path +
                        '/caller_action.py "UpdateAgentInterfaces" "{HOST.NAME}"'
                    }
                }]
            },
            'auth': api.auth_token,
            'id': 1
        })
        request = urllib2.Request(api.api_url, post,
                                  {"Content-Type": "application/json-rpc"})
        contents = urllib2.urlopen(request)
Example #8
0
 def HostStatusChange(self):
     api = ZabbixApi(self.api_url, self.username, self.password)
     api.user_login()
     api.host_status_change(self.host_name, 0)
Example #9
0
 def GetServerHost(self):
     api = ZabbixApi(self.api_url, self.username, self.password)
     api.user_login()
     host_list = api.get_host_list()
     self.host_name = host_list[0]
Example #10
0
    def suggest_candidate_proxy(self):
        """
        ToDo
        """
        api = ZabbixApi(self.api_url, self.username, self.password)
        api.user_login()
        proxies = api.get_proxy_list()

        ipmi_ip = api.get_interface_ipaddress(self.host_name, '3')
        setting = self.rule_data
        proxy_name = ''
        for j in setting:
            flag = ''
            if proxy_name != '':
                break
            for i in [0, 1, 2, 3]:
                ip = int(ipmi_ip.split(".")[i])
                from_ip = int(setting[j]["from"].split(".")[i])
                to_ip = int(setting[j]["to"].split(".")[i])
                if flag == 'from' and from_ip < ip:
                    proxy_name = j
                    break
                elif flag == 'from' and from_ip == ip:
                    if i == 3:
                        proxy_name = j
                        break
                    else:
                        flag == 'from'
                        continue
                elif flag == 'from' and from_ip > ip:
                    break
                elif flag == 'to' and to_ip < ip:
                    break
                elif flag == 'to' and to_ip == ip:
                    if i == 3:
                        proxy_name = j
                        break
                    else:
                        flag == 'to'
                        continue
                elif flag == 'to' and to_ip > ip:
                    proxy_name = j
                    break
                elif i == 3 and ip == to_ip:
                    proxy_name = j
                    break
                elif i == 3 and ip == from_ip:
                    proxy_name = j
                    break
                elif from_ip == to_ip and to_ip == ip:
                    continue
                elif from_ip < to_ip and from_ip == ip:
                    flag = 'from'
                    continue
                elif from_ip < to_ip and to_ip == ip:
                    flag = 'to'
                    continue
                elif from_ip < ip and ip < to_ip:
                    proxy_name = j
                    break
                else:
                    break
        candidate_proxy = proxy_name
        print "Proxy: %s" % candidate_proxy
        return proxy_name
    def suggest_candidate_proxy(self):
        """
        ToDo
        """
        rule_key = "agent_ip"
        api = ZabbixApi(self.api_url, self.username, self.password)
        api.user_login()
        proxies = api.get_proxy_list()

        agent_ip = api.get_interface_ipaddress(self.host_name, '1')

        if self.rule_data.has_key(rule_key):
            print "error"
                        
        proxy_name = ''
        ip = agent_ip.split(".")
        for proxy_define in self.rule_data[rule_key].keys():
            flag = ''
            if proxy_name != '':
                break
            from_ip = self.rule_data[rule_key][proxy_define]["from"].split(".")
            to_ip = self.rule_data[rule_key][proxy_define]["to"].split(".")
            for i in [0, 1, 2, 3]:
                if flag == 'from' and int(from_ip[i]) < int(ip[i]):
                    proxy_name = proxy_define
                    break
                elif flag == 'from' and int(from_ip[i]) == int(ip[i]):
                    if i == 3:
                        proxy_name = proxy_define
                        break
                    else:
                        flag == 'from'
                        continue
                elif flag == 'from' and int(from_ip[i]) > int(ip[i]):
                    break
                elif flag == 'to' and int(to_ip[i]) < int(ip[i]):
                    break
                elif flag == 'to' and int(to_ip[i]) == int(ip[i]):
                    if i == 3:
                        proxy_name = proxy_define
                        break
                    else:
                        flag == 'to'
                        continue
                elif flag == 'to' and int(to_ip[i]) > int(ip[i]):
                    proxy_name = proxy_define
                    break
                elif i == 3 and int(ip[i]) == int(to_ip[i]):
                    proxy_name = proxy_define
                    break
                elif i == 3 and int(ip[i]) == int(from_ip[i]):
                    proxy_name = proxy_define
                    break
                elif int(from_ip[i]) == int(to_ip[i]) and int(to_ip[i]) == int(ip[i]):
                    continue
                elif int(from_ip[i]) < int(to_ip[i]) and int(from_ip[i]) == int(ip[i]):
                    flag = 'from'
                    continue
                elif int(from_ip[i]) < int(to_ip[i]) and int(to_ip[i]) == int(ip[i]):
                    flag = 'to'
                    continue
                elif int(from_ip[i]) < int(ip[i]) and int(ip[i]) < int(to_ip[i]):
                    proxy_name = proxy_define
                    break
                else:
                    break
        print "Proxy: %s" % proxy_name
        return proxy_name
Example #12
0
 def CreateProxy(self):
     api = ZabbixApi(self.api_url, self.username, self.password)
     api.user_login()
     proxy_name = api.get_lastvalue(self.host_name, 'proxy.name')
     api.create_proxy(proxy_name, '0', '', '', '', '')
Example #13
0
    def AgentInterfaces(self):
        api = ZabbixApi(self.api_url, self.username, self.password)
        api.user_login()
        ipaddress = api.get_lastvalue(self.host_name, 'agent.ip')
        api.create_host_interface(self.host_name, '', ipaddress, 1, '10050', 1,
                                  1)

        os_name = commands.getoutput('zabbix_get -s ' + ipaddress +
                                     ' -k system.sw.os | cut -d" " -f1')
        api.attach_templates(self.host_name, 'Custom Template OS ' + os_name)
        api.attach_templates(self.host_name, 'Template Screen')
Example #14
0
    def IpmiInterfaces(self):
        api = ZabbixApi(self.api_url, self.username, self.password)
        api.user_login()
        ipaddress = api.get_lastvalue(self.host_name, 'ssh.run[dcmi]')
        api.create_host_interface(self.host_name, '', ipaddress, 1, '623', 3,
                                  1)
        api.set_ipmi_setting(self.host_name, -1, 4, "admin", "admin")
        api.attach_host_group(self.host_name, "OCP Servers")

        product_name = commands.getoutput('ipmitool -I lanplus -H ' +
                                          ipaddress + ' -U ' + 'admin' +
                                          ' -P ' + 'admin' + ' -A MD5 fru')
        product_name = commands.getoutput(
            'echo "' + product_name +
            '" | grep "Product Name" | cut -d: -f2 | tr -d " "')
        api.attach_templates(self.host_name, 'Template OCP ' + product_name)

        schedule = Schedule(self.host_name)
        proxy_name = schedule.candidate_proxy

        print proxy_name
        if proxy_name != '':
            agent_ipaddress = api.get_interface_ipaddress(self.host_name, '1')
            proxy_ipaddress = api.get_interface_ipaddress(proxy_name, '1')
            commands.getoutput("/bin/sh " + self.path +
                               "/action/update_agent_conf.sh " +
                               agent_ipaddress + " " + proxy_ipaddress)

        api.detach_templates(self.host_name, 'Template SSH Agent')
        api.delete_host_interface(self.host_name)