Created
March 12, 2019 15:50
-
-
Save BoyanHH/60e9f9da0563feaa6191af1d836845ea to your computer and use it in GitHub Desktop.
zabbix api automatic master item/application/dependent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import psutil | |
| import subprocess | |
| import os | |
| import argparse | |
| import sys | |
| import configparser | |
| from collections import OrderedDict | |
| from pyzabbix.api import ZabbixAPI,ZabbixAPIException | |
| def read_config_file(filename): | |
| """Opens the file(required argument when running the program), reads the file, splits by newline and executes check_config() """ | |
| try: | |
| with open(filename,'r') as file: | |
| content = file.read().splitlines() | |
| except IOError: | |
| sys.stderr.write("Could not read file=" + filename) | |
| sys.exit(FILE_IO_ERROR) | |
| return check_config(content) | |
| def check_config(content): | |
| """checks if config contains valid service names - runs systemctl --all --type service and saves it to an array \\ | |
| also appends .service if it's missing""" | |
| valid_process_names = [] | |
| command = "systemctl --all --type service" | |
| try: | |
| output = subprocess.check_output([command], shell=True, stderr=subprocess.PIPE) | |
| except subprocess.CalledProcessError: | |
| sys.stderr.write("Could not run systemctl --all --type service.") | |
| sys.exit(SYSTEMCTL_ALL_ERR); | |
| output = output.decode('utf-8') | |
| for process_name in content: | |
| process = process_name.split(".") | |
| process[0] += str(".service" ) | |
| if process[0] in output: | |
| valid_process_names.append(process[0]) | |
| else: | |
| sys.stderr.write("No such process "+str(process[0])) | |
| exit(NO_SUCH_PROCESS_ERR) | |
| return valid_process_names | |
| def add_application(host_id,app_name,zapi): | |
| try: | |
| item = zapi.application.create( | |
| hostid=host_id, | |
| name=app_name, | |
| ) | |
| except ZabbixAPIException as e: | |
| if "already exists" in str(e): | |
| sys.stderr.write("Application "+app_name+"already exists\n") | |
| return -2 | |
| return -2 | |
| else: | |
| pass | |
| #print("Created app") | |
| for key,val in item.items(): | |
| return(val) | |
| sys.exit(1) | |
| def add_items_to_application(host_id,app_name,zapi,app_id,hosts,master_item_id): | |
| id_of_app = [int(i) for i in app_id] | |
| information_parameters = ["uptime_seconds", "io_write_bytes", "io_read_bytes","memory_rss_bytes","cpu_usage_percent","amount_of_children","io_read_count"] | |
| for par in information_parameters: | |
| name=app_name+"_"+par | |
| try: | |
| item = zapi.item.create( | |
| hostid=host_id, | |
| name=name, | |
| key_=name, | |
| type=18, | |
| master_itemid=master_item_id, | |
| value_type=3, | |
| interfaceid=hosts[0]["interfaces"][0]["interfaceid"], | |
| preprocessing=[{"type":"5","params":name+"\s=\s(.*)\n\\1"}], | |
| applications=id_of_app, | |
| ) | |
| except ZabbixAPIException as e: | |
| sys.stderr.write(e) | |
| sys.exit(1) | |
| def create_master_item(host_id,hosts,zapi): | |
| try: | |
| item = zapi.item.create( ##item only has to be created once. | |
| hostid=host_id, | |
| name='get_service_info_master', | |
| key_='get_service_info[/etc/zabbix/zabbix_agentd.d/list_of_services]', | |
| type=0, | |
| value_type=4, | |
| interfaceid=hosts[0]["interfaces"][0]["interfaceid"], | |
| delay=300, | |
| ) | |
| except ZabbixAPIException as e: | |
| #print(e) | |
| #sys.exit() | |
| return(30607) #item was already created, maybe have this id in a conf file. | |
| else: | |
| return(item["itemids"][0]) | |
| def main(): | |
| """main function that runs the program and connects to zabbix server""" | |
| parser = argparse.ArgumentParser(description='Get information about systemd processes') ##gets argument(filename) | |
| parser.add_argument('filename', type=str,help="file that contains service names") | |
| args = parser.parse_args() | |
| try: | |
| zapi=ZabbixAPI("http://localhost/zabbix",user='Admin',password='zabbix') | |
| zapi.timeout=5.1 | |
| except ZabbixAPIException as e: | |
| sys.stderr.write(e) | |
| sys.exit(1) | |
| except : | |
| sys.stderr.write("Unexpected error:"+str(sys.exc_info()[0])) | |
| sys.exit(1) | |
| host_name = 'Zabbix server' | |
| hosts = zapi.host.get(filter={"host": host_name}, selectInterfaces=["interfaceid"]) | |
| if hosts: | |
| host_id = hosts[0]["hostid"] | |
| master_item_id=create_master_item(host_id,hosts,zapi) | |
| for process in read_config_file(args.filename): | |
| exit_code_zac=add_application(host_id,process,zapi) | |
| if(exit_code_zac!=-2): | |
| add_items_to_application(host_id,process,zapi,exit_code_zac,hosts,master_item_id) | |
| try: | |
| main() | |
| except: | |
| sys.stderr.write("Unexpected error:"+str(sys.exc_info()[0])) | |
| sys.exit(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment