How to generate uptime monthly report in linux?

I wanted to send monthly uptime report to my clients through email. Will any one help me how to generate monthly report.


Solution 1:

There is a tool called "tuptime" that generates information on "uptime" but with a lot more information compared to the single line "uptime" shows.

From their github page:

Basic Installation and usage

Clone the repo

git clone https://github.com/rfrail3/tuptime.git

Copy the tuptime file located under latest/ directory to /usr/bin/ and make it executable

cp tuptime/src/tuptime /usr/bin/tuptime chmod ugo+x /usr/bin/tuptime

Assure that the system pass the prerequisites

python 3.X 

Run first with a privileged user

tuptime

Extra added by me: change the owner and group of typtime to your user with:

sudo chown $USER:$USER /usr/bin/tuptime

and you can run it without sudo/root access.

From the link one of the keypoints it shows is:

  • It register the times in a sqlite database. Any other software can use it. The specs are in the tuptime-manual.txt. Also, it have the option for output the registers in seconds and epoch (-s) or/and in csv format, easy to pipe it to other commands.

So if you can code you could create your own reports. Or even connect the database to something like jasperstudio and create a template.

Otherwise the command tuptime will show this:

System startups:    1   since   21:54:09 24/09/15
System shutdowns:   0 ok   -   0 bad
System uptime:      100.0 %   -   21 minutes and 30 seconds
System downtime:    0.0 %   -   0 seconds
System life:        21 minutes and 30 seconds

Largest uptime:     21 minutes and 30 seconds   from   21:54:09 24/09/15
Shortest uptime:    21 minutes and 30 seconds   from   21:54:09 24/09/15
Average uptime:     21 minutes and 30 seconds

Largest downtime:   0 seconds
Shortest downtime:  0 seconds
Average downtime:   0 seconds

Current uptime:     21 minutes and 30 seconds   since   21:54:09 24/09/15

or tuptime --table will show a tabled output:

No.      Startup Date                              Uptime       Shutdown Date   End                   Downtime

1   10:15:27 08/08/15                          42 seconds   10:16:09 08/08/15    OK                 16 seconds
2   10:16:26 08/08/15                          49 seconds   10:17:15 08/08/15    OK                 16 seconds
3   10:17:32 08/08/15            5 minutes and 47 seconds   10:23:19 08/08/15    OK                 16 seconds
4   10:23:36 08/08/15                           9 seconds   10:23:45 08/08/15   BAD                 42 seconds
5   10:24:28 08/08/15   2 hours, 9 minutes and 27 seconds   12:33:55 08/08/15    OK  41 minutes and 44 seconds
    . . .

The tuptime manual mentioned has loads of good information.

You can send the output to a file by adding >> /home/$USER/Downloads/tuptime.log to the command. That text file could be sent to clients.

Solution 2:

As a quick note to the excellent reply made by Rinzwind about Tuptime.

The package is available in the offical repository, so you can install with:

# apt-get install tuptime

Supossing that you send the report the day 1 of each month, these are the steps:

Get the timestamp of the first day of one month ago from 00:00 hours:

$ date -d "-1 month 00:00" +%s
1514761200

Get the timestamp of the last day of the previous month from 23:59 hours:

$ date -d "this month -1 second 00:00" +%s
1517439599

Use this numbers with the tsince and tuntil arguments:

$ tuptime --tsince 1514761200 --tuntil 1517439599 
System startups:    25   since   00:00:00 01/01/18   until   23:59:59 31/01/18
System shutdowns:   24 ok   -   1 bad
System uptime:      4.84 %   -   1 day, 12 hours, 0 minutes and 24 seconds
System downtime:    95.16 %   -   29 days, 11 hours, 59 minutes and 36 seconds
System life:        31 days, 0 hours, 0 minutes and 0 seconds

Largest uptime:     3 hours, 37 minutes and 41 seconds   from   19:00:15 16/01/18
Shortest uptime:    1 minute and 5 seconds   from   16:40:13 19/01/18
Average uptime:     1 hour, 26 minutes and 25 seconds

Largest downtime:   4 days, 9 hours, 48 minutes and 21 seconds   from   14:11:38 27/01/18
Shortest downtime:  11 seconds   from   16:40:02 19/01/18
Average downtime:   1 day, 4 hours, 19 minutes and 11 seconds

Current uptime:     48 minutes and 19 seconds   since   18:50:03 01/02/18

Now you can get this report, or one of the others available like the table or list format, and send it to the clients.