Sponsored Content
Top Forums Shell Programming and Scripting Help with delaying script and implementing checks before completion Post 302999351 by tomeurp on Sunday 18th of June 2017 05:31:07 AM
Old 06-18-2017
Code Help with delaying script and implementing checks before completion

Hello everyone,

I was wondering if any of you could help me with this. I am an absolute beginner and don't know how to program, but I can follow a tutorial and tweak code sometimes. My understanding of programing is limitted to what for and while loops do, and how if then else logic works. That being said:

My setup is that I have a raspberry pi hooked up to an APC UPS via USB. When mains power goes out, the UPS triggers a "powerout" script which, in turn, sends me an email notification and calls me to my home number and my mobile phone using a SIP account, and a pre-recorded message plays when the call is answered.

I've got this working following various tutorials readily available on the internet.

However, there is a huge problem with this setup as when there is a little fluctuation in the mains power, the UPS turns to battery power in matter of miliseconds and the script is triggered regardless of the fact that power may return in a couple more miliseconds. Therefore I tend to receive calls alerting me of a power failure in the middle of the night and it is really inconvenient.

Therefore, I was hoping that you guys could help me tweak the script so that it waits for, let's say, 10 seconds, then checks again the UPS status, and, only if the UPS is still running on batteries after those 10 secs. places the phone call.

Here is the script which gets triggered when the power goes out:

Code:
# -*- coding: utf-8 -*-
#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when the UPS
# goes on batteries.
# We send an email message to root to notify him.
#

#HOSTNAME=`hostname`
#MSG="$HOSTNAME UPS $1 Power Failure !!!"
#
#(
#   echo "Subject: $MSG"
#   echo " "
#   echo "$MSG"
#   echo " "
#   /sbin/apcaccess status
#) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN

# Added this new script from anites.com
#  ANG 8-23-2014

#!/usr/bin/env python

import smtplib
import email.mime.text
import syslog
import os

syslog.openlog('[UPS]')
def log(msg):
    syslog.syslog(str(msg))

GMAIL_ADDRESS = 'FROM ADDRESS GOES HERE'
GMAIL_PASSWORD = 'GMAIL PASSWORD GOES HERE'

from_email = GMAIL_ADDRESS
to_emails = ["RECIPIENT EMAIL ADDRESS GOES HERE"]  # email address

msg_subject = "MSG SUBJECT GOES HERE"
msg_text = "MSG TEXT GOES HERE"

log(msg_subject)

msg = email.mime.text.MIMEText(msg_text)
msg['Subject'] = msg_subject
msg['From'] = from_email
msg['To'] = ", ".join(to_emails)
s = smtplib.SMTP_SSL('smtp.gmail.com', '465')
s.login(GMAIL_ADDRESS, GMAIL_PASSWORD)
s.sendmail(from_email, to_emails, msg.as_string())
s.quit()

retvalue=os.system("python /etc/apcupsd/alerthome.py")
print retvalue

#exit 0

As you can see, the calling home part is dealt with at the very end of the script by calling a python script (alerthome.py) and Ideally, I would like to pause execution of the script for 10 seconds before that line of code, then somehow check the ups status and, if the power is still out, place the call, but if the power has already returned, exit the script before calling alerthome.py

I imagine it will be easy to pause execution for 10 secs. but the part I really need help with is checking the UPS status.

Also, I guess it could be done by either parsing the results of "cat /var/log/apcupsd.events" where the output is something like:

Code:
[...]
2017-06-08 12:34:16 +0200  Power failure.
2017-06-08 12:34:22 +0200  Running on UPS batteries.
2017-06-08 12:37:04 +0200  Mains returned. No longer on UPS batteries.
2017-06-08 12:37:04 +0200  Power is back. UPS running on mains.
2017-06-08 12:37:14 +0200  Power failure.
2017-06-08 12:37:20 +0200  Running on UPS batteries.
2017-06-08 13:17:57 +0200  Mains returned. No longer on UPS batteries.
2017-06-08 13:17:57 +0200  Power is back. UPS running on mains.

As you can see those lasts ones were legit power outages but it is not always the case. What I would need to do is check whether the last line says either "Power failure." or "Running on UPS batteries." and, in that case place the call, or whether it reads "Mains returned. No longer on UPS batteries." or "Power is back. UPS running on mains." and in that case abort the phone call, but I don't know how to do it.

Another option would be to get the script to call "apcaccess" which produces an output like the following one:
Code:
APC      : 001,028,0733
DATE     : 2017-06-18 11:22:35 +0200  
HOSTNAME : raspberrypi
VERSION  : 3.14.12 (29 March 2014) debian
UPSNAME  : APC-phcy
CABLE    : USB Cable
DRIVER   : USB UPS Driver
UPSMODE  : Stand Alone
STARTTIME: 2017-01-16 20:40:23 +0100  
MODEL    : Smart-UPS 2200 
STATUS   : ONLINE 
BCHARGE  : 100.0 Percent
TIMELEFT : 372.0 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 0 Seconds
ALARMDEL : 30 Seconds
BATTV    : 54.5 Volts
NUMXFERS : 28
XONBATT  : 2017-06-08 12:37:14 +0200  
TONBATT  : 0 Seconds
CUMONBATT: 11121 Seconds
XOFFBATT : 2017-06-08 13:17:57 +0200  
STATFLAG : 0x05000008
MANDATE  : 2016-02-23
SERIALNO : AS1608151711  
NOMBATTV : 48.0 Volts
FIRMWARE : UPS 09.3 / ID=18
END APC  : 2017-06-18 11:22:37 +0200

As you can see, the idea would be similar, I would need to parse this output (which I don't know how to do) to get the value of the "STATUS" line. which will either read ONLINE if the ups is running on mains power, and something else (I don't remember it right now) if it is running on batteries.

Thank you very much in advance. Any help will be much appreciated. Have a nice weekend Smilie

Last edited by Scott; 06-18-2017 at 06:55 AM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script that checks for previous instances running

Hello, I'm trying to write a script that checks for previous instances of the same script which may still be running (this script is scheduled to run every 30 minutes). I want to somehow use the pid from each instance to make sure the previous one isn't running before continuing with my... (5 Replies)
Discussion started by: bd_joy
5 Replies

2. Solaris

Sendmail error delaying relay

When I try to send mail, sendmail delays a lot. After monitoring syslog, I noticed that sendmail starts with this first message... waits a minute and gives the second message... waits another minute and then sends off the email. How do I correct this in sendmail or completly disable it. I'm sending... (0 Replies)
Discussion started by: adelsin
0 Replies

3. Shell Programming and Scripting

Script to perform record format checks

Hi All, I have a requirement to perform the following checks. Input file is a "|" delimited file and looks like this. A|c1|c2|c3|.... B|G1|G2|G3.... C|H1|H2|H3... A|c4|c5|c6|.... B|G4|G5|G6.... C|H4|H5|H6... Now the check is to see if all the "A" records have a corresponding B... (7 Replies)
Discussion started by: gsjdrr
7 Replies

4. Shell Programming and Scripting

Perl script that checks against Future time

Ok, so this may be an unusual request. But I have a certificate that expires sometime in may of 2011. Now, i have to monitor this certificate and alert when the current time is within 30 days of may 20, 2011. #!/usr/bin/perl # use Time::Local; # $sec=59; $min=59; $hours=23; $day=31;... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

[Bash] MD5 Checks with Script.

Hi. I'm triyng to make a Bash Script that checks (recursively) the MD5 from all the files in a certain directory and compare them against some other check that should be already done and saved in a file. I've reached to the point where i have the MD5 from the file and the MD5 that the script... (1 Reply)
Discussion started by: BiFo
1 Replies

6. Shell Programming and Scripting

Script to performs checks

Hi , I need a script which performs below activity I have one file named "testfile" in 9 different directories with same name. I want to perform below action with each testfile of each directory. if ; then mv listfiles listfiles_`date +%b%y` else echo No Such files fi ... (4 Replies)
Discussion started by: sv0081493
4 Replies

7. Shell Programming and Scripting

Script to do the following checks

Hi , I need a script for processing below scenario. I have to check daily by doing ftp IP to check it is logging or not. So i want this activity to be automated such that if login succesful i will get "FTP LOGIN SUCCESS" in a log file and if fails i want the error message in the same log... (1 Reply)
Discussion started by: sv0081493
1 Replies

8. Shell Programming and Scripting

Help with implementing available memory status script

hi , i want write the script which automatically send an alert mail to my mail id when there is low memory available. things which i am able to implement -: i got the output of current memory status into one file . Than i break down the required coloumn and again send it in another file. My... (1 Reply)
Discussion started by: abhinav dixit
1 Replies

9. Shell Programming and Scripting

Script function which checks if itself is already running

Hi All, I have a cron job set up which is set to run every 10 seconds. What I need to do is have the script do a check to see if it is already running such that if it is running it wont fire up additional instances and processes according to its normal process. For example if I have a script... (4 Replies)
Discussion started by: landossa
4 Replies

10. Solaris

Sendmail delaying mails for 45 mins

Hi, I'm trying to send out mails from my server using mailx, however everytime I send one, it appears to be held in the /var/spool/mqueue for 44 mins before being sent. I'm quite new to sendmail, so don't really know where to start with this /var/log/syslog displays the following: Dec 16... (4 Replies)
Discussion started by: badoshi
4 Replies
All times are GMT -4. The time now is 06:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy