Page Menu
Home
GRNET
Search
Configure Global Search
Log In
Files
F1615937
ici.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Mar 22, 5:25 AM
Size
4 KB
Mime Type
text/x-python
Expires
Tue, Mar 24, 5:25 AM (33 m, 35 s)
Engine
blob
Format
Raw Data
Handle
354652
Attached To
rICI livestatus-ici
ici.py
View Options
from
operator
import
itemgetter
import
json
import
urllib2
import
sys
import
os
import
time
# hardcoded for now
MONITORING_URL
=
"194.177.210.168:1337"
def
host_status
(
hostname
,
status_all
):
"""
Does a livestatus query for 'hostname'
and prints the status report.
"""
# need to escape the \n in the URL
api_query
=
"http://"
+
MONITORING_URL
+
\
"/query?q=GET
%20ho
sts
\\
nFilter:%20alias%20=%20"
+
hostname
# handle livestatus-service being down
try
:
req
=
urllib2
.
urlopen
(
api_query
)
.
read
()
except
urllib2
.
URLError
:
sys
.
exit
(
"Could not contact livestatus-service."
)
# handle getting garbage reply
try
:
full
=
json
.
loads
(
req
)
except
ValueError
:
sys
.
exit
(
"Invalid livestatus request."
)
# handle host not having parents registered in icinga
try
:
parent_list
=
"[Parents: "
+
" "
.
join
(
set
(
full
[
0
]
.
get
(
'parents'
)))
+
"]"
except
IndexError
:
parent_list
=
""
print
"Host:
\033
[92m"
+
full
[
0
]
.
get
(
'display_name'
)
+
"
\033
[0m "
+
parent_list
print
"Hostgroups: "
+
" "
.
join
(
full
[
0
]
.
get
(
"groups"
))
# Flexible downtime could get messy here, because the could be an
# item in this list but no an active downtime on the host
if
full
[
0
]
.
get
(
'downtimes_with_info'
):
print
"
\033
[1;36mHost is in downtime: "
+
\
full
[
0
]
.
get
(
'downtimes_with_info'
)[
0
][
2
]
+
\
"("
+
full
[
0
]
.
get
(
'downtimes_with_info'
)[
0
][
1
]
+
")
\033
[0m"
# This could also get tricky, i'm assuming the first comment is
# the one generated by icinga from the downtime command
print
"
\033
[1;36m"
+
full
[
0
]
.
get
(
'comments_with_info'
)[
0
][
2
]
.
split
(
". "
)[
0
]
+
"
\033
[0m"
# Display full checks list (colors W/C/U)
status
=
full
[
0
]
.
get
(
'services_with_fullstate'
)
print
"Status: "
+
str
(
full
[
0
]
.
get
(
'num_services_hard_warn'
))
+
" warn, "
+
\
str
(
full
[
0
]
.
get
(
'num_services_hard_crit'
))
+
" crit, "
+
\
str
(
full
[
0
]
.
get
(
'num_services_hard_unknown'
))
+
\
" unknown, "
+
str
(
full
[
0
]
.
get
(
'num_services'
))
+
" total."
for
i
in
sorted
(
status
,
key
=
itemgetter
(
0
)):
if
i
[
1
]
==
0
:
#OK
if
status_all
:
print
' -
\033
[92m
%s
\033
[0m-
\033
[0;32m
%s
\033
[0m'
%
(
i
[
0
],
i
[
3
])
else
:
pass
elif
i
[
1
]
==
1
:
print
'
\033\t
[93m
%s
-
%s
\033
[0m'
%
(
i
[
0
],
i
[
3
])
elif
i
[
1
]
==
2
:
print
'
\033\t
[91m
%s
-
%s
\033
[0m'
%
(
i
[
0
],
i
[
3
])
elif
i
[
1
]
==
3
:
print
'
\033\t
[94m
%s
-
%s
\033
[0m'
%
(
i
[
0
],
i
[
3
])
def
downtime
(
hostname
,
duration
,
service
,
dt_type
):
"""
Submit a downtime command for a host/service/servicegroup.
Main logic is in the if here to decide which nagios command to submit.
Due to the nature of livestatus-service there is no way to do
error handling here.
Args:
hostname: the fqdn of the host
duration: the duration (in seconds) based on which to submit the command
service: can be a service name or a servicegroup name depending on context
dt_type: specifies whether to submit fixed or flexible downtime command
Returns:
print the output of the livestatus query
"""
cur_time
=
int
(
time
.
time
())
dest_time
=
cur_time
+
int
(
duration
)
user
=
os
.
environ
[
'USER'
]
# livestatus-service doesn't need the first time in brackets, only the
# rest of the command
if
hostname
==
""
:
# hostname being empty means service is servicegroup
ici_cmd
=
"SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;"
+
service
+
";"
+
str
(
cur_time
)
+
";"
+
\
str
(
dest_time
)
+
";1;0;"
+
str
(
duration
)
+
";"
+
user
+
";downtime_by_script"
elif
hostname
!=
""
and
service
==
""
:
# service being empty means downtime for whole host
ici_cmd
=
"SCHEDULE_HOST_SVC_DOWNTIME;"
+
hostname
+
";"
+
str
(
cur_time
)
+
";"
+
\
str
(
dest_time
)
+
";"
+
str
(
dt_type
)
+
";0;"
+
str
(
duration
)
+
";"
+
user
+
\
";downtime_by_script"
else
:
ici_cmd
=
(
"SCHEDULE_SVC_DOWNTIME;"
+
hostname
+
";"
+
service
+
";"
+
str
(
cur_time
)
+
";"
+
\
str
(
dest_time
)
+
";"
+
str
(
dt_type
)
+
";0;"
+
str
(
duration
)
+
\
";"
+
user
+
";downtime_by_script"
)
cmd_query
=
"http://"
+
MONITORING_URL
+
"/cmd?q="
+
ici_cmd
req
=
urllib2
.
urlopen
(
cmd_query
)
.
read
()
print
req
.
rstrip
(
"
\n
"
)
def
ack
(
hostname
,
service
,
notify
,
comment
):
cur_time
=
int
(
time
.
time
())
user
=
os
.
environ
[
'USER'
]
ici_cmd
=
"ACKNOWLEDGE_SVC_PROBLEM;"
+
hostname
+
";"
+
service
+
\
";2;1;"
+
str
(
notify
)
+
";"
+
user
+
";ACK%20By%20Script%20"
+
\
comment
.
replace
(
" "
,
"%20"
)
cmd_query
=
"http://"
+
MONITORING_URL
+
"/cmd?q="
+
ici_cmd
req
=
urllib2
.
urlopen
(
cmd_query
)
.
read
()
print
req
.
rstrip
(
"
\n
"
)
Event Timeline
Log In to Comment