Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
aliveNotifier
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
kpetersn
aliveNotifier
Commits
2b9e5d2c
Commit
2b9e5d2c
authored
10 years ago
by
kpetersn
Browse files
Options
Downloads
Patches
Plain Diff
Handle xml parse error. Added notifyEmail and notifyCombo functions.
parent
ae678f82
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
an.py
+49
-25
49 additions, 25 deletions
an.py
with
49 additions
and
25 deletions
an.py
+
49
−
25
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment