def _disableAlarms(hostid): trigger_get = fowardZ.sendToZabbix(method='trigger.get', params={'hostids': hostid, 'output': 'triggerid'}) triggerPrototype_get = fowardZ.sendToZabbix(method='triggerprototype.get', params={'hostids':hostid, 'output':'triggerid'}) triggers = [] triggersPrototype = [] for trigger in trigger_get['result']: triggers.append({'triggerid': trigger['triggerid'], 'status': '1'}) disabled = fowardZ.sendToZabbix(method='trigger.update', params=triggers) disabledPrototype = dict(result = True) if triggerPrototype_get: for triggerPrototype in triggerPrototype_get['result']: triggersPrototype.append({'triggerid': triggerPrototype['triggerid'], 'status':'1'}) disabledPrototype = fowardZ.sendToZabbix(method='triggerprototype.update', params=triggersPrototype) if 'result' in disabled and 'result' in disabledPrototype: return 'Disable operation was successful.' else: raise InvalidParamsError('It wasnt able to disable alarms on hostid "{0}"'.format(hostid))
def enableAlarms(host): exist = hostExists(host) if exist is not False: trigger_get = fowardZ.sendToZabbix(method='trigger.get', params={'hostids': exist['hostid'], 'output': 'triggerid'}) triggerPrototype_get = fowardZ.sendToZabbix(method='triggerprototype.get', params={'hostids': exist['hostid'], 'output': 'triggerid'}) triggers = list() triggersPrototype = list() for trigger_host in trigger_get['result']: triggers.append({'triggerid': trigger_host['triggerid'], 'status': '0'}) enabled = fowardZ.sendToZabbix(method='trigger.update', params=triggers) enabledPrototype = dict(result = True) if triggerPrototype_get: for triggerPrototype in triggerPrototype_get['result']: triggersPrototype.append({'triggerid': triggerPrototype['triggerid'], 'status':'0'}) enabledPrototype = fowardZ.sendToZabbix(method='triggerprototype.update', params=triggersPrototype) if 'result' in enabled and 'result' in enabledPrototype: return 'Enable operation was successful.' else: raise InvalidParamsError('It wasnt able to enable alarms on host "{0}"'.format(host)) else: raise InvalidParamsError('Host "{0}" doesnt exist.'.format(host))
def enableAlarms(host): exist = hostExists(host) if exist is not False: trigger_get = fowardZ.sendToZabbix(method='trigger.get', params={ 'hostids': exist['hostid'], 'output': 'triggerid' }) triggerPrototype_get = fowardZ.sendToZabbix( method='triggerprototype.get', params={ 'hostids': exist['hostid'], 'output': 'triggerid' }) triggers = list() triggersPrototype = list() for trigger_host in trigger_get['result']: triggers.append({ 'triggerid': trigger_host['triggerid'], 'status': '0' }) enabled = fowardZ.sendToZabbix(method='trigger.update', params=triggers) enabledPrototype = dict(result=True) if triggerPrototype_get: for triggerPrototype in triggerPrototype_get['result']: triggersPrototype.append({ 'triggerid': triggerPrototype['triggerid'], 'status': '0' }) enabledPrototype = fowardZ.sendToZabbix( method='triggerprototype.update', params=triggersPrototype) if 'result' in enabled and 'result' in enabledPrototype: return 'Enable operation was successful.' else: raise InvalidParamsError( 'It wasnt able to enable alarms on host "{0}"'.format(host)) else: raise InvalidParamsError('Host "{0}" doesnt exist.'.format(host))
def auth(self): result = fowardZ.sendToZabbix(method='user.login', params={ 'user': self.user, 'password': self.password }) #print 'Admin login result: ',result self.token = result['result']
def hostgroup(self, name): if self.isName(name): result = fowardZ.sendToZabbix(method="hostgroup.get", params={'filter': {'name': name}}) if 'result' in result and result['result']: return result['result'][0]['groupid'] else: raise InvalidParamsError('Hostgroup not found: \'{0}\' . '.format(name)) else: return name
def template(self, name): if self.isName(name): result = fowardZ.sendToZabbix(method="template.get", params={'filter': {'host': name}}) if 'result' in result and result['result']: # and 'templateid' in result['result'][0]['templateid']: return result['result'][0]['templateid'] else: raise InvalidParamsError('Template not found: \'{0}\' . '.format(name)) else: return name
def enableMonitors(host): exist = hostExists(host) if exist is not False: update = fowardZ.sendToZabbix(method='host.update', params={'hostid': exist['hostid'], 'status': '0'}) if 'result' in update: return 'Enable operation was successful.' else: raise InvalidParamsError('It wasnt able to enable monitors on host "{0}"'.format(host)) else: raise InvalidParamsError('Host "{0}" doesnt exist.'.format(host))
def getAllTriggersAlarming(): triggerCached = cache_get('triggerTelao', cache_options['triggerGet']['name']) if triggerCached: return json.loads(triggerCached) elif cache_get('updatingCache', cache_options['updates']['name']) == 'True': while cache_get('updatingCache', cache_options['updates']['name']) == 'True': time.sleep(0.3) else: return json.loads( cache_get('triggerTelao', cache_options['updates']['name'])) else: if cache_exists('updatingCache', cache_options['updates']['name']): cache_update('updatingCache', 'True', cache_options['updates']['expiration_time'], cache_options['updates']['name']) else: cache_set('updatingCache', 'True', cache_options['updates']['expiration_time'], cache_options['updates']['name']) admin = Admin() zbx_admin_token = admin.auth() triggers = fowardZ.sendToZabbix(method='trigger.get', params={ 'selectHosts': ["name"], 'selectGroups': ['groups'], 'selectLastEvent': ['lastEvent', 'acknowledged'], 'expandComment': 1, 'expandDescription': 1, 'only_true': 1, 'output': 'extend' }, auth=zbx_admin_token) cache_set('triggerTelao', json.dumps(triggers), cache_options['triggerGet']['expiration_time'], cache_options['triggerGet']['name']) cache_update('updatingCache', 'False', cache_options['updates']['expiration_time'], cache_options['updates']['name']) return triggers
def _disableAlarms(hostid): trigger_get = fowardZ.sendToZabbix(method='trigger.get', params={ 'hostids': hostid, 'output': 'triggerid' }) triggerPrototype_get = fowardZ.sendToZabbix(method='triggerprototype.get', params={ 'hostids': hostid, 'output': 'triggerid' }) triggers = [] triggersPrototype = [] for trigger in trigger_get['result']: triggers.append({'triggerid': trigger['triggerid'], 'status': '1'}) disabled = fowardZ.sendToZabbix(method='trigger.update', params=triggers) disabledPrototype = dict(result=True) if triggerPrototype_get: for triggerPrototype in triggerPrototype_get['result']: triggersPrototype.append({ 'triggerid': triggerPrototype['triggerid'], 'status': '1' }) disabledPrototype = fowardZ.sendToZabbix( method='triggerprototype.update', params=triggersPrototype) if 'result' in disabled and 'result' in disabledPrototype: return 'Disable operation was successful.' else: raise InvalidParamsError( 'It wasnt able to disable alarms on hostid "{0}"'.format(hostid))
def disableMonitors(host): exist = hostExists(host) if exist is not False: update = fowardZ.sendToZabbix(method='host.update', params={ 'hostid': exist['hostid'], 'status': '1' }) if 'result' in update: return 'Disable operation was successful.' else: raise InvalidParamsError( 'It wasnt able to disable monitors on host "{0}"'.format(host)) else: raise InvalidParamsError('Host "{0}" doesnt exist.'.format(host))
def hostExists(host): if not isinstance(host, basestring): raise InvalidParamsError('Host parameter must be a string.') result_host_get = fowardZ.sendToZabbix(method="host.get", params={'filter': {'name': host}, #'selectMacros': 'extend', #'selectParentTemplates': 'templateid', #'selectGroups': 'groupid', 'output': ['host', 'name', 'hostid'], 'selectInterfaces': 'extend'}, auth=None) if 'result' in result_host_get and result_host_get['result']: return result_host_get['result'][0] elif 'error' in result_host_get: raise InvalidParamsError('Error on check {0} existence.'.format(host)) else: return False
def deleteMonitors(host): exist = hostExists(host) date = datetime.today().strftime('%y%m%d%H%M%S') if exist is not False: deleted_name = '_' + date + '_' + exist['name'] deleted_host = '_' + date + '_' + exist['host'] update = fowardZ.sendToZabbix(method="host.update", params={'hostid': exist['hostid'], 'host': deleted_host, 'name': deleted_name, 'status': '1', 'groups': [ {'groupid': n2i.hostgroup(GROUP_NOT_EXIST)} ]}) if 'result' in update: return 'Delete operation was successful' else: raise InvalidParamsError('It wasnt able to delete Host "{0}"'.format(host)) else: raise InvalidParamsError('Host "{0}" doesnt exist.'.format(host))
def createMonitor(returning_graphs, monitor_type, hostname, visible_name, ip, proxy=None, groups=None, templates=None, macros=None, inventory=None): if groups is None: groups = [] if templates is None: templates = [] if macros is None: macros = {} templates_structure = [] groups_structure = [] macros_structure = [] for template in templates: templates_structure.append({'templateid': n2i.template(template)}) for group in sorted(set(groups)): groups_structure.append({'groupid': n2i.hostgroup(group)}) for macro, value in macros.iteritems(): if value is not None: macros_structure.append({'macro': macro, 'value': value}) if monitor_type == 'snmp': interfaces_structure = [{'type': '2', # snmp 'main': '1', 'useip': '1', 'ip': ip, 'dns': '', 'port': SNMP_PORT }] elif monitor_type == 'agent': if re.match("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip) is not None: interfaces_structure = [{'type': '1', # agent 'main': '1', 'useip': '1', 'ip': ip, 'dns': '', 'port': AGENT_PORT }] else: interfaces_structure = [{'type': '1', # agent 'main': '1', 'useip': '0', 'ip': '', 'dns': ip, 'port': AGENT_PORT }] else: raise InvalidParamsError('Monitoring type \'{0}\' not supported.'.format(monitor_type)) exist = hostExists(visible_name) if visible_name else hostExists(hostname) # exist = hostExists(visible_name) proxy = n2i.proxy(proxy) if not exist: create = fowardZ.sendToZabbix(method="host.create", params={ 'host': hostname, 'name': visible_name, 'interfaces': interfaces_structure, 'proxy_hostid': proxy, 'groups': groups_structure, 'templates': templates_structure, 'macros': macros_structure, 'inventory': inventory} ) else: raise InvalidParamsError('"{0}" already exists in Zabbix Database.'.format(hostname)) if 'result' in create: if returning_graphs == 'returnallgraphs': graphs = generateGraphs(visible_name) if visible_name else generateGraphs(hostname) if graphs: return graphs else: return 'Create operation was successful, but there are no graphs to show.' elif returning_graphs == 'defaultreturn': return 'Create operation was successful' else: return create else: raise InvalidParamsError(create['error']['data'])