wolfgangam
Posts: 6
Joined: Thu Nov 03, 2016 5:39 pm

Downtime check

Thu Nov 03, 2016 5:48 pm

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

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Downtime check

Thu Nov 03, 2016 7:20 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Downtime check

Thu Nov 03, 2016 7:23 pm

sudo apt-get uprecords uprecords-cgi

Sorted.

It would be quite easy to update the code in uprecords to do the email stuff.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Downtime check

Thu Nov 03, 2016 8:29 pm

Ha. Sometimes i regret posting when there's an easy answer!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

wolfgangam
Posts: 6
Joined: Thu Nov 03, 2016 5:39 pm

Re: Downtime check

Fri Nov 04, 2016 3:56 pm

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...

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Downtime check

Fri Nov 04, 2016 9:14 pm

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
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

wolfgangam
Posts: 6
Joined: Thu Nov 03, 2016 5:39 pm

Re: Downtime check

Sat Nov 05, 2016 2:23 pm

Any other software that will do Downtime calc?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Downtime check

Sat Nov 05, 2016 4:19 pm

You could write something very quickly based on my method...
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

wolfgangam
Posts: 6
Joined: Thu Nov 03, 2016 5:39 pm

Re: Downtime check

Sat Nov 05, 2016 4:35 pm

elParaguayo wrote:You could write something very quickly based on my method...
I definitely will if there is no software available. thank you

epoch1970
Posts: 5203
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Downtime check

Sun Nov 06, 2016 3:38 pm

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...)
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

wolfgangam
Posts: 6
Joined: Thu Nov 03, 2016 5:39 pm

Re: Downtime check

Tue Nov 08, 2016 5:00 pm

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

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Downtime check

Tue Nov 08, 2016 6:31 pm

Try putting the full path to the txt file in your script.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

wolfgangam
Posts: 6
Joined: Thu Nov 03, 2016 5:39 pm

Re: Downtime check

Wed Nov 09, 2016 3:59 pm

elParaguayo wrote:Try putting the full path to the txt file in your script.
Thank you!

Return to “Python”