From 6ab1e756a6267bee392984e1ce26332b4ef66b79 Mon Sep 17 00:00:00 2001 From: Stephen Soltesz Date: Mon, 12 Jan 2009 20:31:54 +0000 Subject: [PATCH] improvements. added site delete to zabbixsync added confirm_ids to model --- monitor-server.init | 6 +++-- monitor/database/zabbixapi/model.py | 40 ++++++++++++++++++++++++----- zabbix/zabbixsite.py | 7 +++-- zabbix/zabbixsync.py | 32 ++++++++++++++++++++--- 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/monitor-server.init b/monitor-server.init index f45360b..a194ebe 100644 --- a/monitor-server.init +++ b/monitor-server.init @@ -77,7 +77,7 @@ function check_user_and_db() fi # Create/update the unprivileged database user and password - if [ -z "$PLC_MONITOR_DBPASSWORD" || "$PLC_MONITOR_DBPASSWORD" = "None" ] ; then + if [[ -z "$PLC_MONITOR_DBPASSWORD" || "$PLC_MONITOR_DBPASSWORD" = "None" ]] ; then # Zabbix doesn't like plain uuidgen passwords PLC_MONITOR_DBPASSWORD=$( uuidgen | md5sum - | awk '{print $1}' ) plc-config --category=plc_monitor --variable=dbpassword --value="$PLC_MONITOR_DBPASSWORD" --save=$local_config $local_config @@ -219,6 +219,7 @@ cachetime=60 # Evaluated as true or false [commandline] cachecalls=True +embedded=False echo=False debug=False @@ -412,7 +413,8 @@ case "$1" in # turn off zabbix server, etc. before writing to the db. service plc stop monitor - $MONITORPATH/zabbix/zabbixsync.py --setupglobal &> /var/log/monitor-server + $MONITORPATH/zabbix/zabbixsync.py --setupids &> /var/log/monitor-server + $MONITORPATH/zabbix/zabbixsync.py --setupglobal 2>&1 >> /var/log/monitor-server # import any templates check_zabbix_templates_and_import diff --git a/monitor/database/zabbixapi/model.py b/monitor/database/zabbixapi/model.py index 35784e6..74407f9 100644 --- a/monitor/database/zabbixapi/model.py +++ b/monitor/database/zabbixapi/model.py @@ -235,6 +235,7 @@ class ZabbixEntity(ZabbixSerialize): fieldname = self._descriptor.auto_primarykey index = IDs.get_by(table_name=tablename, field_name=fieldname) if not index: + print "NEW IDs index INSIDE INIT" index = IDs(table_name=tablename, field_name=fieldname, nodeid=0, nextid=10) index.flush() index.nextid = index.nextid + 1 @@ -736,7 +737,6 @@ class User(ZabbixEntity): # parent of media ug_row = UsersGroups.get_by(usrgrpid=group.usrgrpid, userid=self.userid) if ug_row is not None: ug_row.delete() - #ug_row.flush() return class UsrGrp(ZabbixEntity): @@ -790,10 +790,42 @@ class UsrGrp(ZabbixEntity): ug_row = UsersGroups.get_by(userid=user.userid, usrgrpid=self.usrgrpid) if ug_row is not None: ug_row.delete() - #ug_row.flush() return +def confirm_ids(): + fields = { + 'scripts' : 'scriptid', + 'usrgrp' : 'usrgrpid', + 'users' : 'userid', + 'media' : 'mediaid', + 'users_groups' : 'id', + 'groups' : 'groupid', + 'rights' : 'rightid', + 'drules' : 'druleid', + 'dchecks' : 'dcheckid', + 'actions' : 'actionid', + 'conditions' : 'conditionid', + 'operations' : 'operationid', + 'opconditions' : 'opconditionid', + } + need_to_flush = False + + for tablename in fields.keys(): + fieldname = fields[tablename] + + index = IDs.get_by(table_name=tablename, field_name=fieldname) + if not index: + print "NEW IDs index INSIDE confirm_ids" + index = IDs(table_name=tablename, field_name=fieldname, nodeid=0, nextid=10) + index.flush() + need_to_flush=True + + if need_to_flush: + zab_session.flush() + + setup_all() +confirm_ids() def get_zabbix_class_from_name(name): em = get_zabbix_entitymap() @@ -820,7 +852,3 @@ class OperationConditionNotAck(object): operator=defines.CONDITION_OPERATOR_EQUAL, value=0 ) # NOT_ACK return o - -#import md5 -#u = User(alias="stephen.soltesz@gmail.com", name="stephen.soltesz@gmail.com", surname="", passwd=md5.md5("test").hexdigest(), url="", autologin=0, autologout=900, lang="en_gb", refresh=30, type=1, theme="default.css") -#u.flush() diff --git a/zabbix/zabbixsite.py b/zabbix/zabbixsite.py index 07ad9d4..d6e52cc 100755 --- a/zabbix/zabbixsite.py +++ b/zabbix/zabbixsite.py @@ -97,8 +97,9 @@ def setup_global(): if zabbixserver: # TODO: verify that this works. it has failed once on fresh # install... not sure why. - print "Removing default Zabbix server entry" - zabbixserver.delete() +# print "Removing default Zabbix server entry" +# zabbixserver.delete() + zabbixserver.host="unused default server" # NOTE: creating a host and assigning a template cannot work # due to the crazy item, trigger, action @@ -181,7 +182,9 @@ def setup_site(loginbase, techemail, piemail, iplist): # HOST GROUP plc_host_group = HostGroup.find_or_create(name="MyPLC Hosts") + print "myplc groupid: ", plc_host_group.groupid site_host_group = HostGroup.find_or_create(name=HOSTGROUP_NAME % loginbase) + print "site groupid: ", site_host_group.groupid plctemplate = Host.get_by(host="Template_Linux_PLC_Host") escalation_action_name = ESCALATION_ACTION_NAME % loginbase discovery_action_name = DISCOVERY_ACTION_NAME % loginbase diff --git a/zabbix/zabbixsync.py b/zabbix/zabbixsync.py index e35798c..ffef361 100755 --- a/zabbix/zabbixsync.py +++ b/zabbix/zabbixsync.py @@ -1,12 +1,14 @@ #!/usr/bin/python import sys +import os import site from monitor.wrapper import plc, plccache from monitor import database import zabbixsite from monitor.database.dborm import zab_session as session +from monitor.database.zabbixapi.model import confirm_ids, HostGroup plcdb = plccache.l_sites # database.dbLoad("l_plcsites") @@ -33,6 +35,8 @@ def add_loginbase(loginbase): pis = plc.getPIEmails(loginbase) iplist = get_site_iplist(loginbase) + os.system("""echo '%s' | tr ',' '\n' >> /usr/share/monitor/nodelist.txt""" % iplist ) + print "zabbixsite.setup_site('%s', %s, %s, '%s')" % (loginbase,techs, pis, iplist) zabbixsite.setup_site(loginbase, techs, pis, iplist) @@ -40,7 +44,9 @@ if __name__=="__main__": from monitor import parser as parsermodule parser = parsermodule.getParser(['cacheset']) - parser.set_defaults( setupglobal=False, syncsite=True, site=None) + parser.set_defaults( setupglobal=False, syncsite=True, site=None, setupids=False) + parser.add_option("", "--setupids", action="store_true", dest="setupids", + help="Setup global IDs.") parser.add_option("", "--setupglobal", action="store_true", dest="setupglobal", help="Setup global settings.") parser.add_option("", "--nosite", action="store_false", dest="syncsite", @@ -49,6 +55,16 @@ if __name__=="__main__": help="Sync only given site name.") opts = parsermodule.parse_args(parser) + os.system("""echo '' > /usr/share/monitor/nodelist.txt""") + + if opts.setupids: + # Not sure why, but this doesn't work if we continue. so exit. + # This step only needs to be called once, but there is no harm in + # calling it multiple times. + confirm_ids() + session.flush() + sys.exit(0) + if opts.setupglobal: zabbixsite.setup_global() session.flush() @@ -59,10 +75,20 @@ if __name__=="__main__": if opts.site: query.update({'login_base' : opts.site}) + # ADD SITES sites = api.GetSites(query, ['login_base']) - for site in sites[:10]: + site_api_list = [ site['login_base'] for site in sites ] + for site in sites[:20]: add_loginbase(site['login_base']) session.flush() - # TODO: for any removed site that is in the db, call zabbixsite.delete_site() + if not opts.site: + # NOTE: for all sites in DB but not API, call zabbixsite.delete_site() + hg_list = filter(lambda x: '_hostgroup' in x.name, HostGroup.query.all() ) + site_db_list = [ hg.name[:-len('_hostgroup')] for hg in hg_list ] + in_db_not_plc = set(site_db_list) - set(site_api_list) + for login_base in in_db_not_plc: + print "Deleting %s" % login_base + zabbixsite.delete_site(site['login_base']) + -- 2.43.0