Page 1 of 1

Downtime check

Posted: Thu Nov 03, 2016 5:48 pm
by wolfgangam
Hello,

I made a simple script to send me an email every-time the raspberry boots up, that way I can know when I get an email that there was a power outage in my house.

I'm trying to improve it by having it send me how long was it down for but I can't find a way to do it.


Thanks

Re: Downtime check

Posted: Thu Nov 03, 2016 7:20 pm
by elParaguayo
Just an idea: Have your script write the current time to a file, say, every minute. The script can start on boot. The first thing it does is look at the file and see the last time recorded. You can calculate the current difference between the time now and that time and send your email. The script can then start writing the current time to the file again in a loop.

Sorry about formatting, on my phone.

Re: Downtime check

Posted: Thu Nov 03, 2016 7:23 pm
by DougieLawson
sudo apt-get uprecords uprecords-cgi

Sorted.

It would be quite easy to update the code in uprecords to do the email stuff.

Re: Downtime check

Posted: Thu Nov 03, 2016 8:29 pm
by elParaguayo
Ha. Sometimes i regret posting when there's an easy answer!

Re: Downtime check

Posted: Fri Nov 04, 2016 3:56 pm
by wolfgangam
DougieLawson wrote:sudo apt-get uprecords uprecords-cgi

Sorted.

It would be quite easy to update the code in uprecords to do the email stuff.
Hi there, I installed uprecord-cgi and I'm not sure it works like I want it to.

I read the man a bit and ran "uprecords -sdB", this is what I got the first time :

Code: Select all

     #               Uptime |        Last downtime                       Boot up
----------------------------+---------------------------------------------------
->   1     0 days, 00:09:40 |   0 days, 00:-05:-40      Fri Nov  4 17:20:45 2016
     2     0 days, 00:09:37 |    0 days, 00:00:-23      Fri Nov  4 17:16:48 2016
     3     2 days, 07:28:28 |     0 days, 00:00:00      Wed Nov  2 09:48:43 2016
unplugged the raspberry for few minutes and ran it again :

Code: Select all

     #               Uptime |        Last downtime                       Boot up
----------------------------+---------------------------------------------------
->   1     0 days, 00:04:23 |     0 days, 00:12:43      Fri Nov  4 17:33:08 2016
     2     0 days, 00:03:36 |   0 days, 00:-09:-36      Fri Nov  4 17:16:49 2016
     3     0 days, 00:09:37 |    0 days, 00:00:-23      Fri Nov  4 17:16:48 2016
     4     2 days, 07:28:28 |     0 days, 00:00:00      Wed Nov  2 09:48:43 2016

and third time after more time unplugged

Code: Select all

     #               Uptime |        Last downtime                       Boot up
----------------------------+---------------------------------------------------
->   1     0 days, 00:04:29 |     0 days, 00:27:11      Fri Nov  4 17:47:36 2016
     2     0 days, 00:03:35 |   0 days, 00:-03:-35      Fri Nov  4 17:16:50 2016
     3     0 days, 00:03:36 |   0 days, 00:-09:-36      Fri Nov  4 17:16:49 2016
     4     0 days, 00:09:37 |    0 days, 00:00:-23      Fri Nov  4 17:16:48 2016
     5     2 days, 07:28:28 |     0 days, 00:00:00      Wed Nov  2 09:48:43 2016
:


It does not seems right...

Re: Downtime check

Posted: Fri Nov 04, 2016 9:14 pm
by DougieLawson
It does the same thing on one of my Raspberries, looks like a bug in the way uprecords calculates "Last downtime".

There's some open issues at: https://github.com/rpodgorny/uptimed/issues

Re: Downtime check

Posted: Sat Nov 05, 2016 2:23 pm
by wolfgangam
Any other software that will do Downtime calc?

Re: Downtime check

Posted: Sat Nov 05, 2016 4:19 pm
by elParaguayo
You could write something very quickly based on my method...

Re: Downtime check

Posted: Sat Nov 05, 2016 4:35 pm
by wolfgangam
elParaguayo wrote:You could write something very quickly based on my method...
I definitely will if there is no software available. thank you

Re: Downtime check

Posted: Sun Nov 06, 2016 3:38 pm
by epoch1970
I would rather use a stateless system, like apinger between 2 hosts.
Time-of-day is not a reliable information at boot time, esp. on embedded platforms like Pi.

(eg. to know the "true" boot time, wait for NTP to have acquired sync state and clock delta to be small enough, then convert time to epoch, subtract current uptime, and convert that back to time-of-day...)

Re: Downtime check

Posted: Tue Nov 08, 2016 5:00 pm
by wolfgangam
I wrote a code that will write the epoch time to a file and put the script in cron to run every minute but I can't figure out why cron would not run it...
I tried both bash and python scripts, both work when I ran manually but not with cron..

Python:

Code: Select all

#!/usr/bin/python
import time
import datetime
new_epoch_time = int(time.time())
f = open("epochtime.txt","w")
f.write("%s" % new_epoch_time)
f.close()
Bash:

Code: Select all

#!/bin/bash
date +%s > epochtime.txt
Cron:

Code: Select all

* * * * * /usr/bin/python /home/pi/scripts/epochtime.py >/dev/null 2>&1

Re: Downtime check

Posted: Tue Nov 08, 2016 6:31 pm
by elParaguayo
Try putting the full path to the txt file in your script.

Re: Downtime check

Posted: Wed Nov 09, 2016 3:59 pm
by wolfgangam
elParaguayo wrote:Try putting the full path to the txt file in your script.
Thank you!