def get_hosts(self, host_names): """ Get hosts by host names curl -v -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "host.get","params": {"output": "extend", "filter": {"host": ["tahoe device4"]}},"auth": "8725a8b9977280d879b2bf7449160064","id": 1}' http://192.10.0.60:8000/zabbix/api_jsonrpc.php; Return: {"jsonrpc":"2.0","result":[{"hostid":"10112","proxy_hostid":"0","host":"tahoe device4", "status":"0","disable_until":"0","error":"","available":"0","errors_from":"0","lastaccess":"0","ipmi_authtype":"0","ipmi_privilege":"2","ipmi_username":"","ipmi_password":"","ipmi_disable_until":"0","ipmi_available":"0","snmp_disable_until":"0","snmp_available":"0", "maintenanceid":"0","maintenance_status":"0","maintenance_type":"0","maintenance_from":"0","ipmi_errors_from":"0", "snmp_errors_from":"0","ipmi_error":"","snmp_error":"","jmx_disable_until":"0","jmx_available":"0","jmx_errors_from":"0","jmx_error":"", "name":"tahoe device4","flags":"0","templateid":"0","description":"","tls_connect":"1","tls_accept":"1","tls_issuer":"", "tls_subject":"", "tls_psk_identity":"","tls_psk":""}],"id":1} Input: * host_names: a list host name Output: * hosts: a list of host info ** hostinfo: a dict *** hostid: host id *** host: host name *** name: host name ...... """ request_method = "host.get" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": { "output": "extend", "filter": { "host": host_names } }, "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix get hosts by host names failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix get hosts by host names failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix get hosts by host names failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] hosts = None if len(result) > 0: hosts = result return hosts
def get_userinfo_by_name(self, username): """ Get user info by username: curl -v -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "user.get","params": {"output": "extend","selectMedias": "extend", "filter": {"alias": ["guoleiqiang"]}},"auth": "8725a8b9977280d879b2bf7449160064","id": 1}' http://192.10.0.60:8000/zabbix/api_jsonrpc.php; Return: {"jsonrpc":"2.0","result":[{"userid":"3","alias":"guoleiqiang","name":"New","surname":"User","url":"","autologin":"******","autologout":"0", "lang":"en_GB","refresh":"30","type":"1","theme":"default","attempt_failed":"0","attempt_ip":"","attempt_clock":"0","rows_per_page":"50", "medias":[{"mediaid":"1","userid":"3","mediatypeid":"1","sendto":"*****@*****.**","active":"0","severity":"63","period":"1-7,00:00-24:00"}]}],"id":1} Input: * username: user login name Output: * userinfo: dict ** userid : user id ** alias : user login name ** medias : list, meanning SMS, E-mail and other contact information ...... """ request_method = "user.get" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": { "output": "extend", "selectMedias": "extend", "filter": { "alias": [username] } }, "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix get user info by username failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix get user info by username failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix get user info by username failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] user = None if len(result) > 0: user = result[0] return user
def create_user(self, username, user_group_id, smss=[], emails=[]): """ Create user: curl -v -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "user.create","params": [{"alias": "John","passwd": "John","usrgrps": [{"usrgrpid": "13"}],"user_medias": [{"mediatypeid": "1","sendto": "*****@*****.**","active": 0,"severity": 63,"period": "1-7,00:00-24:00"}]}],"auth": "8725a8b9977280d879b2bf7449160064","id":1}' http://192.10.0.60:8000/zabbix/api_jsonrpc.php; Return: {"jsonrpc":"2.0","result":{"userids":["5"]},"id":1} Input: * username: user login name * user_group_id: user group id * smss: sms list * emails: emails list Output: * userid : user id """ request_method = "user.create" medias = pack_usermedias(smss, emails) password = random_chars(6) data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": { "alias": username, "passwd": password, "usrgrps": [{ "usrgrpid": user_group_id }], "user_medias": medias }, "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix create user failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix create user failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix create user failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] return result["userids"][0]
def get_actions_by_hostid(self, host_id): """ Get actions by host id: curl -v -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "action.get","params": {"output": "extend","selectOperations": "extend","selectRecoveryOperations": "extend","selectFilter": "extend","hostids": ["10113"],"filter": {"eventsource": 0}},"auth": "8725a8b9977280d879b2bf7449160064","id": 1}' http://192.10.0.60:8000/zabbix/api_jsonrpc.php; Return: {"jsonrpc":"2.0","result":[{"actionid":"13","name":"guoleiqiang`s tahoe device tds too high","eventsource":"0","status":"0","esc_period":"120","def_shortdata":"{TRIGGER.NAME}: {TRIGGER.STATUS}","def_longdata":"{TRIGGER.NAME}: {TRIGGER.STATUS}\r\nLast value: {ITEM.LASTVALUE}\r\n\r\n{TRIGGER.URL}\r\n\r\nOriginal event ID: {EVENT.ID}","r_shortdata":"","r_longdata":"","maintenance_mode":"1","filter":{"evaltype":"3","formula":"A and (B or C)","conditions":[{"conditiontype":"3","operator":"2","value":"tds","value2":"","formulaid":"A"},{"conditiontype":"3","operator":"2","value":"tahoe device3","value2":"","formulaid":"B"},{"conditiontype":"3","operator":"2","value":"tahoe device5","value2":"","formulaid":"C"}],"eval_formula":"A and (B or C)"},"operations":[{"operationid":"23","actionid":"13","operationtype":"0","esc_period":"0","esc_step_from":"1","esc_step_to":"1","evaltype":"0","recovery":"0","opconditions":[],"opmessage":{"operationid":"23","default_msg":"0","subject":"{TRIGGER.NAME}: {TRIGGER.STATUS}","message":"\u3010\u8d1e\u6cc9\u3011\u95ee\u9898\uff1a{TRIGGER.NAME}\uff0c\u5f53\u524d\u503c\uff1a {ITEM.LASTVALUE}\uff0c\u4e8b\u4ef6\u7f16\u53f7\uff1a{EVENT.ID}\uff0c\u8bf7\u77e5\u6089\uff0c\u8c22\u8c22","mediatypeid":"4"},"opmessage_grp":[],"opmessage_usr":[{"operationid":"23","userid":"3"}]}],"recoveryOperations":[]}],"id":1} Input: * user_id: user id Output: * actions: is a list of action json format """ request_method = "action.get" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": { "output": "extend", "selectOperations": "extend", "selectRecoveryOperations": "extend", "selectFilter": "extend", "hostids": [host_id], "filter": { "eventsource": 0 } }, "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix get actions by host id failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix get actions by host id failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix get actions by host id failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] return result
def get_template_id(self, template_name): """ Get template id by template name: curl -v -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "template.get","params": {"output": ["templateid"],"filter": {"host": ["Tahoe device template"]}},"auth": "8725a8b9977280d879b2bf7449160064","id": 1}' http://192.10.0.60:8000/zabbix/api_jsonrpc.php; Return: {"jsonrpc":"2.0","result":[{"templateid":"10105"}],"id":1} Input: * template_name: template name Output: * template_id: template id """ request_method = "template.get" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": { "output": ["templateid"], "filter": { "host": [template_name] } }, "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix set template id failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix set template id failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix set template id failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] return result[0]["templateid"]
def update_action_condition(self, action_id, conditions): """ Update action condition. Input: * action_id: action id * conditions: the conditions is list of update action need to send Output: * actionid: created action id """ request_method = "action.update" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": { "actionid": action_id, "filter": { "evaltype": "0", "conditions": conditions } }, "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix update action condition failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix update action condition failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix update action condition failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] return result["actionids"][0]
def get_auth(self): """ Get auth: curl -v -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "user.login","params": {"user": "******","password": "******"},"id": 1,"auth": null}' http://192.10.0.60:8000/zabbix/api_jsonrpc.php; Return: {"jsonrpc":"2.0","result":"8725a8b9977280d879b2bf7449160064","id":1} """ request_method = "user.login" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": { "user": self.user, "password": self.password }, "id": 1, "auth": None } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix get auth info failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix get auth info failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix get auth info failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] return result
def delete_actions(self, action_ids): """ Delete actions by action_ids Input: * action_ids: action id list Output: * action_ids: deleted action ids """ request_method = "action.delete" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": action_ids, "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix delete action failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix delete action failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix delete action failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] return result["actionids"]
def create_action(self, item_name, host_ids, user_name, user_id, notification_mode=NOTIFICATION_MODE_SMS): """ Input: * item_name: monitoring item name * host_ids: host id list * user_name: the user name * user_id: the user id * notification_mode: default NOTIFICATION_MODE_ALL, other:NOTIFICATION_MODE_EMAIL, NOTIFICATION_MODE_SMS Output: * actionid: created action id """ action_name = "%s`s device occur %s exception action" % (user_name, item_name) conditions = pack_action_conditions(host_ids, item_name, None) operations = pack_action_operations(user_id, notification_mode) request_method = "action.create" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": [{ "name": action_name, "eventsource": 0, "status": 0, "esc_period": 120, "def_shortdata": NOTIFICATION_SUBJECT, "def_longdata": NOTIFICATION_MESSAGE, "filter": { "evaltype": "0", "conditions": conditions }, "operations": operations }], "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix create action failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix create action failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix create action failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] return result["actionids"][0]
def create_host(self, host_name, host_group_id, host_template_id): """ Create host: curl -v -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "host.create","params": [{"host": "tahoe device4","interfaces": [{"type": 1,"main": 1,"useip": 1,"ip": "127.0.0.1","dns": "","port": "10050"}],"groups": [{"groupid": "8"}],"templates": [{"templateid": "10105"}],"inventory_mode": 0,"inventory": {"macaddress_a": "ab:3c:6d:5c"}}],"auth": "8725a8b9977280d879b2bf7449160064","id": 1}' http://192.10.0.60:8000/zabbix/api_jsonrpc.php; Return: {"jsonrpc":"2.0","result":{"hostids":["10112"]},"id":1} Input: * host_name: host name * host_group_id: host group id * host_template_id: host template id Output: * host_id: created host id """ request_method = "host.create" data = { "jsonrpc": zabbix_config.ZABBIX_JSONRPC_VERSION, "method": request_method, "params": [{ "host": host_name, "interfaces": [{ "type": 1, "main": 1, "useip": 1, "ip": "127.0.0.1", "dns": "", "port": "10050" }], "groups": [{ "groupid": host_group_id }], "templates": [{ "templateid": host_template_id }] }], "id": 1, "auth": self.auth } headers = {'Content-Type': 'application/json'} try: rsp = requests.post(zabbix_config.ZABBIX_URL, headers=headers, json=data) except Exception as e: logger.error("zabbix create host failure: %s" % e.message) raise if rsp.status_code != ZABBIX_HTTP_SUCCESS: logger.error("zabbix create host failure: code(%d), " "reason: %s" % (rsp.status_code, rsp.reason)) raise ZabbixException(rsp.reason) if rsp.json().has_key(ZABBIX_RESPONSE_KEY_ERROR): error = rsp.json()[ZABBIX_RESPONSE_KEY_ERROR] reason = error['message'] + " " + error['data'] logger.error("zabbix create host failure: code(%d), " "reason: %s" % (error['code'], reason)) raise ZabbixException(reason) result = rsp.json()[ZABBIX_RESPONSE_KEY_RESULT] return result["hostids"][0]