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
2b9e5d2c
Commit
2b9e5d2c
authored
May 15, 2014
by
kpetersn
Browse files
Handle xml parse error. Added notifyEmail and notifyCombo functions.
parent
ae678f82
Changes
1
Hide whitespace changes
Inline
Side-by-side
an.py
View file @
2b9e5d2c
...
...
@@ -7,32 +7,50 @@
import
urllib
import
xml.etree.ElementTree
as
ET
import
os
import
sys
# for time.asctime()
#!import time
# needed for notifyOSX()
import
os
osx_command
=
"/Users/kpetersn/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier"
osx_url
=
"http://bcda.xray.aps.anl.gov/cgi-bin/ioc_alive.cgi?eng=Peterson"
### Stuff to customize ###
engineer
=
"Peterson"
# period should match cron job period
period
=
60
# needed for notifyOSX()
osx_command
=
"/Users/kpetersn/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier"
# needed for notifyEmail()
recipient
=
"kmpeters@anl.gov"
def
notifyConsole
(
ioc
,
state
,
time_str
):
print
"[%s] %s is %s"
%
(
time_str
,
ioc
,
state
)
def
notifyEmail
(
ioc
,
state
,
time_str
):
subject
=
"%s is %s"
%
(
ioc
,
state
)
message
=
"Snapshot time: %s
\n
%s"
%
(
time_str
,
eng_url
)
email_command
=
"echo
\"
%s
\"
| mailx -s
\"
%s
\"
%s"
%
(
message
,
subject
,
recipient
)
os
.
system
(
email_command
)
def
notifyOSX
(
ioc
,
state
,
time_str
):
notify_command
=
"%s -title
\"
%s is %s
\"
-message
\"
%s
\"
-open
\"
%s
\"
"
%
(
osx_command
,
ioc
,
state
,
time_str
,
osx
_url
)
notify_command
=
"%s -title
\"
%s is %s
\"
-message
\"
%s
\"
-open
\"
%s
\"
"
%
(
osx_command
,
ioc
,
state
,
time_str
,
eng
_url
)
os
.
system
(
notify_command
)
def
notifyCombo
(
ioc
,
state
,
time_str
):
notifyOSX
(
ioc
,
state
,
time_str
)
notifyEmail
(
ioc
,
state
,
time_str
)
# set notification function
notify
=
notifyOSX
#!notify = notifyConsole
#!notify = notifyEmail
notify
=
notifyOSX
#!notify = notifyCombo
##########################
alive_url
=
"http://bcda.xray.aps.anl.gov/cgi-bin/alivexml.cgi"
eng_url
=
"http://bcda.xray.aps.anl.gov/cgi-bin/ioc_alive.cgi?eng=%s"
%
engineer
hb_period
=
10
ioc_list
=
{}
...
...
@@ -46,30 +64,34 @@ f.close()
# Parse the xml string
#!print "Parsing XML"
root
=
ET
.
fromstring
(
xml_string
)
try
:
root
=
ET
.
fromstring
(
xml_string
)
except
ET
.
ParseError
:
print
"Invalid XML file"
sys
.
exit
(
1
)
# Get the date and time
time_elem
=
root
.
find
(
'time'
)
time_str
=
time_elem
.
text
# 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
#!
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)
...
...
@@ -116,3 +138,5 @@ iocs.sort()
for
ioc
in
iocs
:
notify
(
ioc
,
ioc_list
[
ioc
],
time_str
)
sys
.
exit
(
0
)
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