Added infovacuum model
[monitor.git] / zabbix / zabbixsite.py
1 #!/usr/bin/python
2
3 from os import getcwd
4 from os.path import dirname, exists, join
5 import sys
6 import md5
7
8 from monitor import config
9 from monitor.database.dborm import zab_session as session
10 from monitor.database.zabbixapi.model import *
11 from monitor.database.zabbixapi.emailZabbix import *
12 from monitor.database.zabbixapi import defines
13
14
15
16 HOSTGROUP_NAME="%s_hostgroup"
17 USERGROUP_NAME="%s_usergroup"
18         
19 DISCOVERY_RULE_NAME="discovery rule for %s"
20 DISCOVERY_ACTION_NAME="Auto-discover %s action"
21 ESCALATION_ACTION_NAME="Escalation Action for %s"
22
23 def delete_site(loginbase):
24
25         # get host group, usrgrp
26         # get all users in usrgrp, delete each
27         usergroupname = USERGROUP_NAME % loginbase
28         hostgroupname = HOSTGROUP_NAME % loginbase
29         discovery_action_name = DISCOVERY_ACTION_NAME % loginbase
30         discovery_rule_name = DISCOVERY_RULE_NAME % loginbase
31         escalation_action_name = ESCALATION_ACTION_NAME % loginbase
32
33         ug = UsrGrp.get_by(name=usergroupname)
34         if ug:
35                 for user in ug.user_list:
36                         # remove user from group, if a member of no other groups, 
37                         # delete user.
38                         #user.delete()
39                         pass
40                 ug.delete()
41
42         hg = HostGroup.get_by(name=hostgroupname)
43         if hg: 
44                 # figure out how to delete all the hosts...
45                 # NOTE: hosts are listed in hg.host_list
46                 for host in hg.host_list:
47                         host.delete()
48                 hg.delete()
49
50         # delete dr
51         dr = DiscoveryRule.get_by(name=discovery_rule_name)
52         if dr: dr.delete()
53
54         da = Action.get_by(name=discovery_action_name)
55         if da: da.delete()
56
57         ea = Action.get_by(name=escalation_action_name)
58         if ea: ea.delete()
59
60         return
61
62
63 def setup_global():
64         # GLOBAL:
65         #       update mediatype for email.
66         ############################### MAIL
67         print "checking for MediaType Email"
68         mediatype = MediaType.get_by(description="Email")
69         if not mediatype:
70                 print "ERROR:  There is no defined media type for 'Email'"
71                 raise Exception("No Email Media type in Zabbix db")
72
73         print "checking for correct configuration"
74         mediatype = MediaType.get_by(smtp_email=config.from_email)
75         if not mediatype:
76                 # NOTE: assumes smtp server is local to this machine.
77                 print "updating email server configuration"
78                 mediatype.smtp_server='localhost'
79                 mediatype.smtp_helo=".".join(config.MONITOR_HOSTNAME.split('.')[1:])
80                 mediatype.smtp_email=config.from_email
81
82         ############################# EMAIL
83         mailtxt.reformat({'hostname' : config.MONITOR_HOSTNAME, 
84                                           'support_email' : config.support_email})
85
86         ############################### CENTRAL SERVER
87         print "checking zabbix server host info"
88         zabbixserver = Host.get_by(host="ZABBIX Server")
89         if zabbixserver:
90                 print "UPDATING Primary Zabbix server entry"
91                 zabbixserver.host=config.MONITOR_HOSTNAME
92                 zabbixserver.ip=config.MONITOR_IP
93                 zabbixserver.dns=config.MONITOR_HOSTNAME
94                 zabbixserver.useip=1
95
96         ############################ DEFAULT TEMPLATES
97         # pltemplate - via web, xml import
98         # TODO: os.system("curl --post default_templates.xml")
99
100         ##################### SCRIPTS 
101         ## TODO: add calls to check/reset the boot states.
102         print "checking scripts"
103         script1 = Script.find_or_create(name="RebootNode",
104                                                                         set_if_new = {
105                                                                                 'command':"/usr/share/monitor-server/reboot.py {HOST.CONN}",
106                                                                                 'host_access':3 # r/w)
107                                                                         })
108         script2 = Script.find_or_create(name="NMap",
109                                                         set_if_new = {
110                                                                 'command':"/usr/bin/nmap -A {HOST.CONN}",
111                                                                 'host_access':2 # r/o)
112                                                 })
113         return
114
115 def setup_site(loginbase, techemail, piemail, iplist):
116
117         # TODO: send a message when host is discovered.
118
119         # TODO: update 'discovered' hosts with dns name.
120         # TODO: remove old nodes that are no longer in the plcdb.
121         # TODO: remove old users that are no longer in the plcdb.
122         # TODO: consider creating two user groups for Tech & PI emails
123
124         BI_WEEKLY_ESC_PERIOD = int(60*60*24)
125         BI_WEEKLY_ESC_PERIOD = int(60) # testing...
126
127         # User Group
128         site_user_group = UsrGrp.find_or_create(name=USERGROUP_NAME % loginbase)
129         for user in set(techemail + piemail + [config.cc_email]):
130                 if not user: continue
131                 # USER
132                 u = User.find_or_create(alias=user, type=1,
133                                                                 set_if_new={'passwd' : md5.md5(user).hexdigest()},
134                                                                 # exec_if_new avoids creating a Media object that
135                                                                 # will not actually be used, if the user already exists
136                                                                 exec_if_new=lambda obj: \
137                                                                 obj.media_list.append( Media(mediatypeid=1, sendto=user)))
138
139                 if site_user_group not in u.usrgrp_list:
140                         u.append_group(site_user_group)
141
142         # HOST GROUP
143         plc_host_group = HostGroup.find_or_create(name="MyPLC Hosts")
144         site_host_group = HostGroup.find_or_create(name=HOSTGROUP_NAME % loginbase)
145         plctemplate = Host.get_by(host="Template_Linux_PLHost")
146         escalation_action_name = ESCALATION_ACTION_NAME % loginbase
147         discovery_action_name = DISCOVERY_ACTION_NAME % loginbase
148         discovery_rule_name = DISCOVERY_RULE_NAME % loginbase
149
150         # ADD hg to ug
151         if site_host_group not in site_user_group.hostgroup_list:
152                 site_user_group.append_hostgroup(site_host_group)
153
154         # DISCOVERY RULE & CHECK
155         dr = DiscoveryRule.find_or_create(name=discovery_rule_name,
156                           delay=3600,
157                           proxy_hostid=0,
158                           set_if_new = {'iprange':iplist},
159                           exec_if_new=lambda obj: \
160                                 obj.discoverycheck_list.append( DiscoveryCheck(type=9, 
161                                                                                 key_="system.uname", ports=10050) )
162                         )
163         if dr.iprange != iplist:
164                 if len(iplist) < 255:
165                         dr.iprange = iplist
166                 else:
167                         raise Exception("iplist length is too long!")
168                 
169
170         # DISCOVERY ACTION for these servers
171         a = Action.find_or_create(name=discovery_action_name,
172                         eventsource=defines.EVENT_SOURCE_DISCOVERY,
173                         status=defines.DRULE_STATUS_ACTIVE,
174                         evaltype=defines.ACTION_EVAL_TYPE_AND_OR)
175         if len(a.actioncondition_list) == 0:
176                 a.actioncondition_list=[
177                                         # Host IP Matches
178                                         ActionCondition(
179                                                 conditiontype=defines.CONDITION_TYPE_DHOST_IP,
180                                                 operator=defines.CONDITION_OPERATOR_EQUAL,
181                                                 value=iplist),
182                                         # AND, Service type is Zabbix agent
183                                         ActionCondition(
184                                                 conditiontype=defines.CONDITION_TYPE_DSERVICE_TYPE,
185                                                 operator=defines.CONDITION_OPERATOR_EQUAL,
186                                                 value=defines.SVC_AGENT),
187                                         # AND, Received system.uname value like 'Linux'
188                                         ActionCondition(
189                                                 conditiontype=defines.CONDITION_TYPE_DVALUE,
190                                                 operator=defines.CONDITION_OPERATOR_LIKE,
191                                                 value="Linux"),
192                                         # AND, Discovery status is Discover
193                                         ActionCondition(
194                                                 conditiontype=defines.CONDITION_TYPE_DSTATUS,
195                                                 operator=defines.CONDITION_OPERATOR_EQUAL,
196                                                 value=defines.DOBJECT_STATUS_DISCOVER),
197                                 ]
198                                 # THEN
199                 a.actionoperation_list=[
200                                         # Add Host
201                                         ActionOperation(
202                                                 operationtype=defines.OPERATION_TYPE_HOST_ADD,
203                                                 object=0, objectid=0,
204                                                 esc_period=0, esc_step_from=1, esc_step_to=1),
205                                         # Add To Group PLC Hosts
206                                         ActionOperation(
207                                                 operationtype=defines.OPERATION_TYPE_GROUP_ADD,
208                                                 object=0, objectid=plc_host_group.groupid,
209                                                 esc_period=0, esc_step_from=1, esc_step_to=1),
210                                         # Add To Group LoginbaseSiteGroup
211                                         ActionOperation(
212                                                 operationtype=defines.OPERATION_TYPE_GROUP_ADD,
213                                                 object=0, objectid=site_host_group.groupid,
214                                                 esc_period=0, esc_step_from=1, esc_step_to=1),
215                                         # Link to Template 'Template_Linux_Minimal'
216                                         ActionOperation(
217                                                 operationtype=defines.OPERATION_TYPE_TEMPLATE_ADD,
218                                                 object=0, objectid=plctemplate.hostid,
219                                                 esc_period=0, esc_step_from=1, esc_step_to=1),
220                                 ]
221         else:
222                 # TODO: verify iplist is up-to-date
223                 pass
224
225         # ESCALATION ACTION for these servers
226         ea = Action.find_or_create(name=escalation_action_name,
227                         eventsource=defines.EVENT_SOURCE_TRIGGERS,
228                         status=defines.ACTION_STATUS_ENABLED,
229                         evaltype=defines.ACTION_EVAL_TYPE_AND_OR,
230                         esc_period=BI_WEEKLY_ESC_PERIOD,        # three days
231                         recovery_msg=1,
232                         set_if_new={
233                                 'r_shortdata':"Thank you for maintaining {HOSTNAME}!",
234                                 'r_longdata': mailtxt.thankyou_nodeup, }
235                         )
236         if len(ea.actioncondition_list) == 0:
237                         # THEN this is a new entry
238                 print "SETTING UP ESCALATION ACTION"
239                 ea.actioncondition_list=[
240                                 ActionCondition(conditiontype=defines.CONDITION_TYPE_TRIGGER_VALUE, 
241                                                                 operator=defines.CONDITION_OPERATOR_EQUAL, 
242                                                                 value=defines.TRIGGER_VALUE_TRUE),
243                                 ActionCondition(conditiontype=defines.CONDITION_TYPE_TRIGGER_NAME, 
244                                                                 operator=defines.CONDITION_OPERATOR_LIKE, 
245                                                                 value="is unreachable"),
246                                 ActionCondition(conditiontype=defines.CONDITION_TYPE_HOST_GROUP, 
247                                                                 operator=defines.CONDITION_OPERATOR_EQUAL, 
248                                                                 value=site_host_group.groupid),
249                         ]
250                 ea.actionoperation_list=[
251                                 # STAGE 1
252                                 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE,
253                                         shortdata=mailtxt.nodedown_one_subject,
254                                         longdata=mailtxt.nodedown_one,
255                                         object=defines.OPERATION_OBJECT_GROUP, 
256                                         objectid=site_user_group.usrgrpid, 
257                                         esc_period=0, esc_step_to=3, esc_step_from=3, 
258                                         operationcondition_list=[ OperationConditionNotAck() ] ),
259                                 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE,
260                                         shortdata=mailtxt.nodedown_one_subject,
261                                         longdata=mailtxt.nodedown_one,
262                                         object=defines.OPERATION_OBJECT_GROUP, 
263                                         objectid=site_user_group.usrgrpid, 
264                                         esc_period=0, esc_step_to=7, esc_step_from=7, 
265                                         operationcondition_list=[ OperationConditionNotAck() ] ),
266                                 # STAGE 2
267                                 ActionOperation(operationtype=defines.OPERATION_TYPE_COMMAND, 
268                                         esc_step_from=10, esc_step_to=10, 
269                                         esc_period=0,
270                                         shortdata="",
271                                         longdata="%s:/usr/share/monitor-server/checkslices.py {HOSTNAME} disablesite" % config.MONITOR_HOSTNAME, 
272                                         operationcondition_list=[ OperationConditionNotAck() ]),
273                                 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE, 
274                                         shortdata=mailtxt.nodedown_two_subject,
275                                         longdata=mailtxt.nodedown_two,
276                                         esc_step_from=10, esc_step_to=10, 
277                                         esc_period=0, 
278                                         object=defines.OPERATION_OBJECT_GROUP, 
279                                         objectid=site_user_group.usrgrpid, 
280                                         operationcondition_list=[ OperationConditionNotAck() ] ), 
281                                 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE, 
282                                         shortdata=mailtxt.nodedown_two_subject,
283                                         longdata=mailtxt.nodedown_two,
284                                         esc_step_from=14, esc_step_to=14, 
285                                         esc_period=0, 
286                                         object=defines.OPERATION_OBJECT_GROUP, 
287                                         objectid=site_user_group.usrgrpid, 
288                                         operationcondition_list=[ OperationConditionNotAck() ] ), 
289
290                                 # STAGE 3
291                                 ActionOperation(operationtype=defines.OPERATION_TYPE_COMMAND, 
292                                         esc_step_from=17, esc_step_to=17, 
293                                         esc_period=0, 
294                                         shortdata="",
295                                         longdata="%s:/usr/share/monitor-server/checkslices.py {HOSTNAME} disableslices" % config.MONITOR_HOSTNAME, 
296                                         # TODO: send notice to users of slices
297                                         operationcondition_list=[ OperationConditionNotAck() ]),
298                                 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE, 
299                                         shortdata=mailtxt.nodedown_three_subject,
300                                         longdata=mailtxt.nodedown_three,
301                                         esc_step_from=17, esc_step_to=17, 
302                                         esc_period=0, 
303                                         object=defines.OPERATION_OBJECT_GROUP, 
304                                         objectid=site_user_group.usrgrpid, 
305                                         operationcondition_list=[ OperationConditionNotAck() ] ), 
306                                 # STAGE 4++
307                                 ActionOperation(operationtype=defines.OPERATION_TYPE_COMMAND, 
308                                         esc_step_from=21, esc_step_to=0, 
309                                         esc_period=int(BI_WEEKLY_ESC_PERIOD*3.5),
310                                         shortdata="",
311                                         longdata="%s:/usr/share/monitor-server/checkslices.py {HOSTNAME} forever" % config.MONITOR_HOSTNAME, 
312                                         operationcondition_list=[ OperationConditionNotAck() ]),
313                                 ActionOperation(operationtype=defines.OPERATION_TYPE_MESSAGE, 
314                                         shortdata=mailtxt.nodedown_four_subject,
315                                         longdata=mailtxt.nodedown_four,
316                                         esc_step_from=21, esc_step_to=0, 
317                                         esc_period=int(BI_WEEKLY_ESC_PERIOD*3.5),
318                                         object=defines.OPERATION_OBJECT_GROUP, 
319                                         objectid=site_user_group.usrgrpid, 
320                                         operationcondition_list=[ OperationConditionNotAck() ] ), 
321                         ]
322
323 if __name__ == "__main__":
324         setup_global()
325         session.flush()