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