4 from os.path import dirname, exists, join
9 from monitor import config
10 from monitor.database.dborm import zab_session as session
11 from monitor.database.zabbixapi.model import *
12 from monitor.database.zabbixapi.emailZabbix import *
13 from monitor.database.zabbixapi import defines
14 from monitor.util.file import *
18 HOSTGROUP_NAME="%s_hostgroup"
19 USERGROUP_NAME="%s_usergroup"
21 DISCOVERY_RULE_NAME="discovery rule for %s"
22 DISCOVERY_ACTION_NAME="Auto-discover %s action"
23 ESCALATION_ACTION_NAME="Escalation Action for %s"
25 def delete_site(loginbase):
27 # get host group, usrgrp
28 # get all users in usrgrp, delete each
29 usergroupname = USERGROUP_NAME % loginbase
30 hostgroupname = HOSTGROUP_NAME % loginbase
31 discovery_action_name = DISCOVERY_ACTION_NAME % loginbase
32 discovery_rule_name = DISCOVERY_RULE_NAME % loginbase
33 escalation_action_name = ESCALATION_ACTION_NAME % loginbase
35 ug = UsrGrp.get_by(name=usergroupname)
37 for user in ug.user_list:
38 # remove user from group, if a member of no other groups,
44 hg = HostGroup.get_by(name=hostgroupname)
46 # figure out how to delete all the hosts...
47 # NOTE: hosts are listed in hg.host_list
48 for host in hg.host_list:
53 dr = DiscoveryRule.get_by(name=discovery_rule_name)
56 da = Action.get_by(name=discovery_action_name)
59 ea = Action.get_by(name=escalation_action_name)
67 # update mediatype for email.
68 ############################### MAIL
69 print "checking for MediaType Email"
70 mediatype = MediaType.get_by(description="Email")
72 print "ERROR: There is no defined media type for 'Email'"
73 raise Exception("No Email Media type in Zabbix db")
75 print "checking for correct configuration"
76 mediatype = MediaType.get_by(smtp_email=config.from_email)
78 mediatype = MediaType.get_by(description="Email")
79 # NOTE: assumes smtp server is local to this machine.
80 print "updating email server configuration"
81 mediatype.smtp_server='localhost'
82 mediatype.smtp_helo=".".join(config.MONITOR_HOSTNAME.split('.')[1:])
83 mediatype.smtp_email=config.from_email
85 ############################# EMAIL
86 mailtxt.reformat({'hostname' : config.MONITOR_HOSTNAME,
87 'support_email' : config.support_email})
89 ############################### CENTRAL SERVER
90 print "checking zabbix server host info"
91 zabbixserver = Host.get_by(host="ZABBIX Server")
93 # TODO: verify that this works. it has failed once on fresh
94 # install... not sure why.
95 print "Removing default Zabbix server entry"
98 # NOTE: creating a host and assigning a template cannot work
99 # due to the crazy item, trigger, action
100 # copying that the php code does during a host add.
101 # NOTE: Instead, reformat any *xml.in templates and import those
102 # during /etc/plc.d/monitor sync
103 for file in glob.glob("%s/zabbix/templates/*.xml.in" config.MONITOR_SCRIPT_ROOT):
104 if 'zabbix_server' in file:
106 args = {'hostname' : config.MONITOR_HOSTNAME, 'ip' : config.MONITOR_IP}
107 dumpFile(file[:-3], buf % args)
110 ##################### SCRIPTS
111 ## TODO: add calls to check/reset the boot states.
112 print "checking scripts"
113 script1 = Script.find_or_create(name="RebootNode",
115 'command':"%s/reboot.py {HOST.CONN}" % config.MONITOR_SCRIPT_ROOT,
116 'host_access':3 # r/w)
118 script2 = Script.find_or_create(name="NMap",
120 'command':"/usr/bin/nmap -A {HOST.CONN}",
121 'host_access':2 # r/o)
125 def setup_site(loginbase, techemail, piemail, iplist):
127 # TODO: send a message when host is discovered.
129 # TODO: update 'discovered' hosts with dns name.
130 # TODO: remove old nodes that are no longer in the plcdb.
131 # TODO: remove old users that are no longer in the plcdb.
132 # TODO: consider creating two user groups for Tech & PI emails
134 # NOTE: setup default valus for EMAIL
135 mailtxt.reformat({'hostname' : config.MONITOR_HOSTNAME,
136 'support_email' : config.support_email})
138 # NOTE: verify arguments
139 if len(iplist) > 255:
140 raise Exception("iplist length is too long!")
142 BI_WEEKLY_ESC_PERIOD = int(60*60*24)
143 #BI_WEEKLY_ESC_PERIOD = int(60) # testing...
146 site_user_group = UsrGrp.find_or_create(name=USERGROUP_NAME % loginbase)
147 for user in set(techemail + piemail + [config.cc_email]):
148 if not user: continue
150 u = User.find_or_create(alias=user, type=1,
151 set_if_new={'passwd' : md5.md5(user).hexdigest()},
152 # exec_if_new avoids creating a Media object that
153 # will not actually be used, if the user already exists
154 exec_if_new=lambda obj: \
155 obj.media_list.append( Media(mediatypeid=1, sendto=user)))
157 if site_user_group not in u.usrgrp_list:
158 u.append_group(site_user_group)
161 plc_host_group = HostGroup.find_or_create(name="MyPLC Hosts")
162 site_host_group = HostGroup.find_or_create(name=HOSTGROUP_NAME % loginbase)
163 plctemplate = Host.get_by(host="Template_Linux_PLC_Host")
164 escalation_action_name = ESCALATION_ACTION_NAME % loginbase
165 discovery_action_name = DISCOVERY_ACTION_NAME % loginbase
166 discovery_rule_name = DISCOVERY_RULE_NAME % loginbase
169 if site_host_group not in site_user_group.hostgroup_list:
170 site_user_group.append_hostgroup(site_host_group)
172 # DISCOVERY RULE & CHECK
173 dr = DiscoveryRule.find_or_create(name=discovery_rule_name,
176 set_if_new = {'iprange':iplist},
177 exec_if_new=lambda obj: \
178 obj.discoverycheck_list.append( DiscoveryCheck(type=9,
179 key_="system.uname", ports=10050) )
181 if dr.iprange != iplist:
185 # DISCOVERY ACTION for these servers
186 a = Action.find_or_create(name=discovery_action_name,
187 eventsource=defines.EVENT_SOURCE_DISCOVERY,
188 status=defines.DRULE_STATUS_ACTIVE,
189 evaltype=defines.ACTION_EVAL_TYPE_AND_OR)
190 if len(a.actioncondition_list) == 0:
191 a.actioncondition_list=[
194 conditiontype=defines.CONDITION_TYPE_DHOST_IP,
195 operator=defines.CONDITION_OPERATOR_EQUAL,
197 # AND, Service type is Zabbix agent
199 conditiontype=defines.CONDITION_TYPE_DSERVICE_TYPE,
200 operator=defines.CONDITION_OPERATOR_EQUAL,
201 value=defines.SVC_AGENT),
202 # AND, Received system.uname value like 'Linux'
204 conditiontype=defines.CONDITION_TYPE_DVALUE,
205 operator=defines.CONDITION_OPERATOR_LIKE,
207 # AND, Discovery status is Discover
209 conditiontype=defines.CONDITION_TYPE_DSTATUS,
210 operator=defines.CONDITION_OPERATOR_EQUAL,
211 value=defines.DOBJECT_STATUS_DISCOVER),
214 a.actionoperation_list=[
217 operationtype=defines.OPERATION_TYPE_HOST_ADD,
218 object=0, objectid=0,
219 esc_period=0, esc_step_from=1, esc_step_to=1),
220 # Add To Group PLC Hosts
222 operationtype=defines.OPERATION_TYPE_GROUP_ADD,
223 object=0, objectid=plc_host_group.groupid,
224 esc_period=0, esc_step_from=1, esc_step_to=1),
225 # Add To Group LoginbaseSiteGroup
227 operationtype=defines.OPERATION_TYPE_GROUP_ADD,
228 object=0, objectid=site_host_group.groupid,
229 esc_period=0, esc_step_from=1, esc_step_to=1),
230 # Link to Template 'Template_Linux_Minimal'
232 operationtype=defines.OPERATION_TYPE_TEMPLATE_ADD,
233 object=0, objectid=plctemplate.hostid,
234 esc_period=0, esc_step_from=1, esc_step_to=1),
237 # TODO: verify iplist is up-to-date
238 # NOTE: len(a.actioncondition_list) > 0
239 ip_condition = a.actioncondition_list[0]
240 assert ip_condition.conditiontype == defines.CONDITION_TYPE_DHOST_IP
241 if ip_condition.value != iplist:
242 ip_condition.value = iplist
244 # ESCALATION ACTION for these servers
245 ea = Action.find_or_create(name=escalation_action_name,
246 eventsource=defines.EVENT_SOURCE_TRIGGERS,
247 status=defines.ACTION_STATUS_ENABLED,
248 evaltype=defines.ACTION_EVAL_TYPE_AND_OR,
249 esc_period=BI_WEEKLY_ESC_PERIOD, # three days
252 'r_shortdata':"Thank you for maintaining {HOSTNAME}!",
253 'r_longdata': mailtxt.thankyou_nodeup, }
255 if len(ea.actioncondition_list) == 0:
256 # THEN this is a new entry
257 print "SETTING UP ESCALATION ACTION"
258 ea.actioncondition_list=[
259 ActionCondition(conditiontype=defines.CONDITION_TYPE_TRIGGER_VALUE,
260 operator=defines.CONDITION_OPERATOR_EQUAL,
261 value=defines.TRIGGER_VALUE_TRUE),
262 ActionCondition(conditiontype=defines.CONDITION_TYPE_TRIGGER_NAME,
263 operator=defines.CONDITION_OPERATOR_LIKE,
264 value="is unreachable"),
265 ActionCondition(conditiontype=defines.CONDITION_TYPE_HOST_GROUP,
266 operator=defines.CONDITION_OPERATOR_EQUAL,
267 value=site_host_group.groupid),
269 ea.actionoperation_list=[
271 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE,
272 shortdata=mailtxt.nodedown_one_subject,
273 longdata=mailtxt.nodedown_one,
274 object=defines.OPERATION_OBJECT_GROUP,
275 objectid=site_user_group.usrgrpid,
276 esc_period=0, esc_step_to=3, esc_step_from=3,
277 operationcondition_list=[ OperationConditionNotAck() ] ),
278 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE,
279 shortdata=mailtxt.nodedown_one_subject,
280 longdata=mailtxt.nodedown_one,
281 object=defines.OPERATION_OBJECT_GROUP,
282 objectid=site_user_group.usrgrpid,
283 esc_period=0, esc_step_to=7, esc_step_from=7,
284 operationcondition_list=[ OperationConditionNotAck() ] ),
286 ActionOperation(operationtype=defines.OPERATION_TYPE_COMMAND,
287 esc_step_from=10, esc_step_to=10,
290 longdata="%s:%s/checkslices.py {HOSTNAME} disablesite" % ( config.MONITOR_HOSTNAME, config.MONITOR_SCRIPT_ROOT ),
291 operationcondition_list=[ OperationConditionNotAck() ]),
292 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE,
293 shortdata=mailtxt.nodedown_two_subject,
294 longdata=mailtxt.nodedown_two,
295 esc_step_from=10, esc_step_to=10,
297 object=defines.OPERATION_OBJECT_GROUP,
298 objectid=site_user_group.usrgrpid,
299 operationcondition_list=[ OperationConditionNotAck() ] ),
300 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE,
301 shortdata=mailtxt.nodedown_two_subject,
302 longdata=mailtxt.nodedown_two,
303 esc_step_from=14, esc_step_to=14,
305 object=defines.OPERATION_OBJECT_GROUP,
306 objectid=site_user_group.usrgrpid,
307 operationcondition_list=[ OperationConditionNotAck() ] ),
310 ActionOperation(operationtype=defines.OPERATION_TYPE_COMMAND,
311 esc_step_from=17, esc_step_to=17,
314 longdata="%s:%s/checkslices.py {HOSTNAME} disableslices" % ( config.MONITOR_HOSTNAME, config.MONITOR_SCRIPT_ROOT ),
315 # TODO: send notice to users of slices
316 operationcondition_list=[ OperationConditionNotAck() ]),
317 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE,
318 shortdata=mailtxt.nodedown_three_subject,
319 longdata=mailtxt.nodedown_three,
320 esc_step_from=17, esc_step_to=17,
322 object=defines.OPERATION_OBJECT_GROUP,
323 objectid=site_user_group.usrgrpid,
324 operationcondition_list=[ OperationConditionNotAck() ] ),
326 ActionOperation(operationtype=defines.OPERATION_TYPE_COMMAND,
327 esc_step_from=21, esc_step_to=0,
328 esc_period=int(BI_WEEKLY_ESC_PERIOD*3.5),
330 longdata="%s:%s/checkslices.py {HOSTNAME} forever" % ( config.MONITOR_HOSTNAME, config.MONITOR_SCRIPT_ROOT),
331 operationcondition_list=[ OperationConditionNotAck() ]),
332 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE,
333 shortdata=mailtxt.nodedown_four_subject,
334 longdata=mailtxt.nodedown_four,
335 esc_step_from=21, esc_step_to=0,
336 esc_period=int(BI_WEEKLY_ESC_PERIOD*3.5),
337 object=defines.OPERATION_OBJECT_GROUP,
338 objectid=site_user_group.usrgrpid,
339 operationcondition_list=[ OperationConditionNotAck() ] ),
342 if __name__ == "__main__":