Merge ~xavpaice/charm-hw-health:LP1838562 into ~nagios-charmers/charm-hw-health:master

Proposed by Xav Paice
Status: Merged
Approved by: Paul Goins
Approved revision: 068f960f8c0b5562c7e2e52de6f97800951fc63b
Merged at revision: 6c29dde4a3f29fc7137619dea3cfe399f196dd79
Proposed branch: ~xavpaice/charm-hw-health:LP1838562
Merge into: ~nagios-charmers/charm-hw-health:master
Diff against target: 60 lines (+25/-14)
1 file modified
src/files/ipmi/cron_ipmi_sensors.py (+25/-14)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Peter Sabaini Approve
Review via email: mp+378128@code.launchpad.net

Commit message

Update ipmi cron job to cater for stale lockfile

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Peter Sabaini (peter-sabaini) wrote :

Some comments inline, otherwise lgtm

review: Approve
Revision history for this message
Xav Paice (xavpaice) wrote :

Updated the change to add the file paths in the error messages trapped.

Revision history for this message
Stuart Bishop (stub) wrote :

Looks good

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 6c29dde4a3f29fc7137619dea3cfe399f196dd79

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/files/ipmi/cron_ipmi_sensors.py b/src/files/ipmi/cron_ipmi_sensors.py
2index a9538af..a11f9fb 100644
3--- a/src/files/ipmi/cron_ipmi_sensors.py
4+++ b/src/files/ipmi/cron_ipmi_sensors.py
5@@ -17,30 +17,41 @@ NAGIOS_ERRORS = {
6
7
8 def gather_metrics():
9- # a child is already running
10+ # Check if a PID file exists
11 if os.path.exists(CHECK_IPMI_PID):
12- return
13+ # is the PID valid?
14+ with open(CHECK_IPMI_PID, 'r') as fd:
15+ PID = fd.read()
16+ if PID not in os.listdir('/proc'):
17+ # PID file is invalid, remove it
18+ os.remove(CHECK_IPMI_PID)
19+ else:
20+ return
21
22 try:
23 with open(CHECK_IPMI_PID, 'w') as fd:
24 fd.write(str(os.getpid()))
25-
26- cmdline = [CMD]
27- if len(sys.argv) > 1:
28- cmdline.extend(sys.argv[1:])
29-
30+ except IOError as e:
31+ # unable to write PID file, can't lock
32+ print("Cannot write lock file {}, error {}".format(CHECK_IPMI_PID, e))
33+ sys.exit(1)
34+
35+ cmdline = [CMD]
36+ if len(sys.argv) > 1:
37+ cmdline.extend(sys.argv[1:])
38+ try:
39 output = subprocess.check_output(cmdline)
40- with open(TMP_OUTPUT_FILE, 'w') as fd:
41- fd.write(output.decode(errors='ignore'))
42- os.rename(TMP_OUTPUT_FILE, OUTPUT_FILE)
43 except subprocess.CalledProcessError as error:
44 output = error.stdout.decode(errors='ignore')
45 with open(TMP_OUTPUT_FILE, 'w') as fd:
46 fd.write('{}: {}'.format(NAGIOS_ERRORS[error.returncode], output))
47- os.rename(TMP_OUTPUT_FILE, OUTPUT_FILE)
48- except PermissionError as error:
49- with (OUTPUT_FILE, 'w') as fd:
50- fd.write('UNKNOWN: {}'.format(error))
51+ try:
52+ with open(TMP_OUTPUT_FILE, 'w') as fd:
53+ fd.write(output)
54+ except IOError as e:
55+ print("Cannot write output file {}, error {}".format(TMP_OUTPUT_FILE, e))
56+ sys.exit(1)
57+ os.rename(TMP_OUTPUT_FILE, OUTPUT_FILE)
58
59 # remove pid reference
60 os.remove(CHECK_IPMI_PID)

Subscribers

People subscribed via source and target branches