Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
kpetersn
aliveNotifier
Commits
ca28fe23
Commit
ca28fe23
authored
May 14, 2014
by
kpetersn
Browse files
Initial commit of an.py, which works on the console and OS X.
parents
Changes
2
Hide whitespace changes
Inline
Side-by-side
.gitignore
0 → 100644
View file @
ca28fe23
.DS_Store
an.py
0 → 100755
View file @
ca28fe23
#!/usr/bin/env python
# TODO:
# Allow engineer to be passed as an argument
# Add other notification functions
# Replace explicit period a time since a dot file was last touched
import
urllib
import
xml.etree.ElementTree
as
ET
import
time
# needed for notifyOSX()
import
os
osx_command
=
"/Users/kpetersn/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier"
### Stuff to customize
engineer
=
"Peterson"
# period should match cron job period
period
=
120
def
notifyConsole
(
ioc
,
state
):
notify_time
=
time
.
asctime
()
print
"[%s] %s is %s"
%
(
notify_time
,
ioc
,
state
)
def
notifyOSX
(
ioc
,
state
):
notify_time
=
time
.
asctime
()
notify_command
=
"%s -title
\"
%s is %s
\"
-message
\"
%s
\"
"
%
(
osx_command
,
ioc
,
state
,
notify_time
)
os
.
system
(
notify_command
)
# set notification function
notify
=
notifyOSX
#!notify = notifyConsole
###
alive_url
=
"http://bcda.xray.aps.anl.gov/cgi-bin/alivexml.cgi"
ioc_list
=
{}
# Get the xml file
#!print "Opening URL"
f
=
urllib
.
urlopen
(
alive_url
)
#!print "Reading XML from URL"
xml_string
=
f
.
read
()
#!print "Closing URL"
f
.
close
()
# Parse the xml string
#!print "Parsing XML"
root
=
ET
.
fromstring
(
xml_string
)
# Indent for making ET.tostring() output readable
def
_indent
(
elem
,
level
=
0
):
'''
Internal method to make the xml file easier to read by humans.
'''
i
=
"
\n
"
+
level
*
" "
if
len
(
elem
):
if
not
elem
.
text
or
not
elem
.
text
.
strip
():
elem
.
text
=
i
+
" "
if
not
elem
.
tail
or
not
elem
.
tail
.
strip
():
elem
.
tail
=
i
for
elem
in
elem
:
_indent
(
elem
,
level
+
1
)
if
not
elem
.
tail
or
not
elem
.
tail
.
strip
():
elem
.
tail
=
i
else
:
if
level
and
(
not
elem
.
tail
or
not
elem
.
tail
.
strip
()):
elem
.
tail
=
i
# Indent for making ET.tostring() output readable
#!_indent(root)
# Extract info from the xml file
for
ioc
in
root
.
iter
(
'IOC'
):
ping_timer
=
-
1
boot_timer
=
-
1
calc_flag
=
False
name
=
ioc
.
attrib
[
'name'
]
#print ET.tostring(ioc)
for
elem
in
ioc
:
if
elem
.
tag
==
'ping_timer'
:
ping_timer
=
int
(
elem
.
text
)
if
elem
.
tag
==
'boot_timer'
:
boot_timer
=
int
(
elem
.
text
)
if
elem
.
tag
==
'env'
:
if
elem
.
attrib
[
'name'
]
==
'ENGINEER'
:
if
elem
.
text
==
engineer
:
# Set calc flag
calc_flag
=
True
if
calc_flag
==
True
:
#!print "name =", name
#!print "ping_timer =", ping_timer
#!print "boot_timer =", boot_timer
#!print "engineer =", engineer
#!print
if
boot_timer
<=
period
:
# ioc just booted
ioc_list
[
name
]
=
'UP'
elif
ping_timer
>
period
:
if
ping_timer
<=
(
2
*
period
):
# ioc went down since last poll
ioc_list
[
name
]
=
'DOWN'
else
:
# ioc has been down for a while
pass
iocs
=
ioc_list
.
keys
()
iocs
.
sort
()
for
ioc
in
iocs
:
notify
(
ioc
,
ioc_list
[
ioc
])
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment