* 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.
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
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
service plc stop monitor
$MONITORPATH/zabbix/zabbixsync.py --setupglobal &> /var/log/monitor-server
+ # import any templates
+ check_templates_and_import
service plc start monitor
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
auto_primarykey='hostgroupid',
)
+class HostsTemplates(ZabbixEntity):
+ using_options(
+ tablename='hosts_templates',
+ autoload=True,
+ auto_primarykey='hosttemplateid',
+ )
+
class Host(ZabbixEntity):
using_options(
tablename='hosts',
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()
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
-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
-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
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 *
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.
# 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
if opts.setupglobal:
zabbixsite.setup_global()
+ session.flush()
if opts.syncsite:
api = plc.getAuthAPI()