From a06427d1c9506ee93888e8f2f80b6c4bdd134527 Mon Sep 17 00:00:00 2001 From: Stephen Soltesz Date: Thu, 20 Nov 2008 00:49:04 +0000 Subject: [PATCH] * add code to import xml templates using curl. * do not attempt to create a host() object in zabbixsite.setup_global, preferring the import mechanism instead. * leave the template code in zabbixapi/model.py to help with deleting hosts eventually. --- monitor-server.init | 43 ++++++++++++++++++++++++++++- monitor/database/zabbixapi/model.py | 29 +++++++++++++++++++ monitor/util/file.py | 11 ++++++++ zabbix.spec | 14 ++++++++-- zabbix/zabbixsite.py | 25 +++++++++++------ zabbix/zabbixsync.py | 1 + 6 files changed, 110 insertions(+), 13 deletions(-) diff --git a/monitor-server.init b/monitor-server.init index 21671bf..fd21161 100644 --- a/monitor-server.init +++ b/monitor-server.init @@ -97,6 +97,45 @@ function check_schema_and_data() fi } +function check_templates_and_import () +{ + # LOG IN + COOKIE_FILE=/tmp/cookiejar.txt + TEMPLATES_DIR=/usr/share/monitor/zabbix/templates + curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \ + --form "enter=Enter" \ + --form "name=Admin" \ + --form "password=zabbix" \ + "http://${PLC_MONITOR_HOST}/zabbix/index.php?login=1" + + deleted=$( grep 'deleted' $COOKIE_FILE ) + if [ -n "$deleted" ] ; then + echo "Login to the zabbix web server failed!!!" + return 1 + fi + + for file in ${TEMPLATES_DIR}/*.xml ; do + # 0 - update , 1 - skip, 0 - add + echo "############### IMPORTING $file" >> /var/log/monitor.log + curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \ + --form "config=1" \ + --form "import_file=@${file}" \ + --form "rules[host][exist]=0" \ + --form "rules[host][missed]=0" \ + --form "rules[template][exist]=0" \ + --form "rules[template][missed]=1" \ + --form "rules[item][exist]=0" \ + --form "rules[item][missed]=0" \ + --form "rules[trigger][exist]=0" \ + --form "rules[trigger][missed]=0" \ + --form "rules[graph][exist]=0" \ + --form "rules[graph][missed]=0" \ + --form "import=Import" \ + "http://${PLC_MONITOR_HOST}/zabbix/exp_imp.php" >> /var/log/monitor.log + done +} + + function check_monitor_conf () { MONITOR_CONFIG=/etc/monitor.conf @@ -239,11 +278,11 @@ case "$1" in MESSAGE=$"Bootstrap Monitoring" dialog "$MESSAGE" - # DATABASE acces, creation, and data loading check_pg_hba check_user_and_db check_schema_and_data + check_templates_and_import # WRITE default /etc/monitor.conf check_monitor_conf @@ -265,6 +304,8 @@ case "$1" in service plc stop monitor $MONITORPATH/zabbix/zabbixsync.py --setupglobal &> /var/log/monitor-server + # import any templates + check_templates_and_import service plc start monitor diff --git a/monitor/database/zabbixapi/model.py b/monitor/database/zabbixapi/model.py index fed0420..3d6bd40 100644 --- a/monitor/database/zabbixapi/model.py +++ b/monitor/database/zabbixapi/model.py @@ -439,6 +439,7 @@ class Right(ZabbixEntity): rights = Table('rights', __metadata__, autoload=True) hostsgroups = Table('hosts_groups', __metadata__, autoload=True) +hoststemplates = Table('hosts_templates', __metadata__, autoload=True) # m2m table between hosts and groups below @@ -449,6 +450,13 @@ class HostsGroups(ZabbixEntity): auto_primarykey='hostgroupid', ) +class HostsTemplates(ZabbixEntity): + using_options( + tablename='hosts_templates', + autoload=True, + auto_primarykey='hosttemplateid', + ) + class Host(ZabbixEntity): using_options( tablename='hosts', @@ -462,8 +470,29 @@ class Host(ZabbixEntity): primaryjoin=lambda: Host.hostid==hostsgroups.c.hostid, secondaryjoin=lambda: HostGroup.groupid==hostsgroups.c.groupid, ) + template_list = ManyToMany( + 'Host', + table=hoststemplates, + foreign_keys=lambda: [hoststemplates.c.hostid, hoststemplates.c.templateid], + primaryjoin=lambda: Host.hostid==hoststemplates.c.hostid, + secondaryjoin=lambda: Host.hostid==hoststemplates.c.templateid, + ) + + def append_template(self, template): + row = HostsTemplates(hostid=self.hostid, templateid=template.hostid) + return template + + def remove_template(self, template): + row = HostsTemplates.get_by(hostid=self.hostid, templateid=template.hostid) + if row is not None: + row.delete() + def delete(self): # NOTE: media objects are automatically handled. + hosts_templates_match = HostsTemplates.query.filter_by(hostid=self.hostid).all() + for row in hosts_templates_match: + row.delete() + hosts_groups_match = HostsGroups.query.filter_by(hostid=self.hostid).all() for row in hosts_groups_match: row.delete() diff --git a/monitor/util/file.py b/monitor/util/file.py index 167186a..43ff968 100644 --- a/monitor/util/file.py +++ b/monitor/util/file.py @@ -17,3 +17,14 @@ def getListFromFile(file): list += [line] return list +def loadFile(file): + f = open(file,'r') + buf = f.read() + f.close() + return buf + +def dumpFile(file, buf): + f = open(file, 'w') + f.write(buf) + f.close() + return diff --git a/zabbix.spec b/zabbix.spec index 3f2e548..479e684 100644 --- a/zabbix.spec +++ b/zabbix.spec @@ -170,11 +170,15 @@ sed -e "s#BASEDIR=/opt/zabbix#BASEDIR=%{_prefix}#g" \ -e "s#PIDFILE=/var/tmp/zabbix_agentd.pid#PIDFILE=%{zabbix_piddir}/zabbix_agentd.pid#g" \ %{zabbix_initdir}/zabbix_agentd > $TMP_FILE cat $TMP_FILE > %{zabbix_initdir}/zabbix_agentd -# TODO: copy to /etc/init.d/ + +# NOTE: Run every runlevel as soon as possible, and stop as late as possible cp %{zabbix_initdir}/zabbix_agentd %{_initrddir} +sed -i -e "s#chkconfig: - 90 10#chkconfig: 2345 12 90#g" \ + %{_initrddir}/zabbix_server rm -f $TMP_FILE +chkconfig --add zabbix_agentd chkconfig zabbix_agentd on %post server @@ -205,10 +209,14 @@ sed -e "s#BASEDIR=/opt/zabbix#BASEDIR=%{_prefix}#g" \ -e "s#PIDFILE=/var/tmp/zabbix_server.pid#PIDFILE=%{zabbix_piddir}/zabbix_server.pid#g" \ %{zabbix_initdir}/zabbix_server > $TMP_FILE cat $TMP_FILE > %{zabbix_initdir}/zabbix_server -cp %{zabbix_initdir}/zabbix_server %{_initrddir} - rm -f $TMP_FILE +# NOTE: Run every runlevel as soon as possible, and stop as late as possible +cp %{zabbix_initdir}/zabbix_server %{_initrddir} +sed -i -e "s#chkconfig: - 90 10#chkconfig: 2345 12 90#g" \ + %{_initrddir}/zabbix_server + +chkconfig --add zabbix_server chkconfig zabbix_server on %post gui diff --git a/zabbix/zabbixsite.py b/zabbix/zabbixsite.py index 678c756..e3bcfb4 100755 --- a/zabbix/zabbixsite.py +++ b/zabbix/zabbixsite.py @@ -4,12 +4,14 @@ from os import getcwd from os.path import dirname, exists, join import sys import md5 +import glob from monitor import config from monitor.database.dborm import zab_session as session from monitor.database.zabbixapi.model import * from monitor.database.zabbixapi.emailZabbix import * from monitor.database.zabbixapi import defines +from monitor.util.file import * @@ -88,15 +90,20 @@ def setup_global(): print "checking zabbix server host info" zabbixserver = Host.get_by(host="ZABBIX Server") if zabbixserver: - print "UPDATING Primary Zabbix server entry" - zabbixserver.host=config.MONITOR_HOSTNAME - zabbixserver.ip=config.MONITOR_IP - zabbixserver.dns=config.MONITOR_HOSTNAME - zabbixserver.useip=1 + print "Removing default Zabbix server entry" + zabbixserver.delete() + + # NOTE: creating a host and assigning a template cannot work + # due to the crazy item, trigger, action + # copying that the php code does during a host add. + # NOTE: Instead, reformat any *xml.in templates and import those + # during /etc/plc.d/monitor sync + for file in glob.glob("/usr/share/monitor/templates/*.xml.in"): + if 'zabbix_server' in file: + buf = loadFile(file) + args = {'hostname' : config.MONITOR_HOSTNAME, 'ip' : config.MONITOR_IP} + dumpFile(file[:-3], buf % args) - ############################ DEFAULT TEMPLATES - # pltemplate - via web, xml import - # TODO: os.system("curl --post default_templates.xml") ##################### SCRIPTS ## TODO: add calls to check/reset the boot states. @@ -143,7 +150,7 @@ def setup_site(loginbase, techemail, piemail, iplist): # HOST GROUP plc_host_group = HostGroup.find_or_create(name="MyPLC Hosts") site_host_group = HostGroup.find_or_create(name=HOSTGROUP_NAME % loginbase) - plctemplate = Host.get_by(host="Template_Linux_PLHost") + plctemplate = Host.get_by(host="Template_Linux_PLC_Host") escalation_action_name = ESCALATION_ACTION_NAME % loginbase discovery_action_name = DISCOVERY_ACTION_NAME % loginbase discovery_rule_name = DISCOVERY_RULE_NAME % loginbase diff --git a/zabbix/zabbixsync.py b/zabbix/zabbixsync.py index 6ab825f..3af935b 100755 --- a/zabbix/zabbixsync.py +++ b/zabbix/zabbixsync.py @@ -50,6 +50,7 @@ if __name__=="__main__": if opts.setupglobal: zabbixsite.setup_global() + session.flush() if opts.syncsite: api = plc.getAuthAPI() -- 2.43.0