Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Below is data taken from my dedicated server:

root@namhost [~]# last
root     pts/0        XXX Tue May 18 09:46   still logged in
root     pts/0        XXX Mon May 17 08:51 - 12:18  (03:26)
reboot   system boot  XXX Mon May 17 08:49         (1+00:59)
root     pts/0        XXX Sun May 16 11:50 - 13:15  (01:25)

root@namhost [~]# last | grep "system boot"
reboot   system boot  2.6.18-164.15.1. Mon May 17 08:49         (1+01:02)
reboot   system boot  2.6.18-164.el5   Tue May 11 04:20         (7+05:31)
reboot   system boot  2.6.18-164.el5   Tue May 11 03:53         (7+05:58)
reboot   system boot  2.6.18-128.el5   Mon Oct  5 22:40          (-3:-50)
....

I need a script that I can run on an hourly basis that will: 1. Calculate the total downtime since the first date 2. The overall downtime percentage 3. Store this data in a file at /home/bla/file.txt, in the following format: TotalDowntime=03:02:02 Average=0.01%

How do I go about doing this?

share|improve this question
    
What do you have so far? –  Dennis Williamson May 18 '10 at 13:23
    
what do you mean by downtown, are you referring to time that no one is logged in and using the system, by CPU, or when the computer is off? –  wag2639 May 18 '10 at 14:48
    
Computer is off. –  coderama May 19 '10 at 8:37
    
Nagios or Zenoss is monitoring software that can do what you want. –  Chris Aug 26 '10 at 19:06

2 Answers 2

If you're trying to figure out when your system was shut down (and I'm not sure why a server would be down at all other than a problem), you can try the following (though I'm sure theres got to be a much better way):

  1. Create a folder somewhere to log stuff (perhaps /var/downtime_logs/)
  2. Create a cronjob to touch a file in that folder touch /var/downtown_logs/`date +%s` every minute (or use $(date +%s) in order to avoid using backticks)
  3. Then count the number of files in that folder
  4. If you compare the date of the first file to the current date, that's how much time you've been keeping stats for and you can do some math to get the rest

I've left this as a community wiki for other people to chime in and finish the rest

share|improve this answer
1  
I presume instead of "test +%s" you mean "date +%s". Also, you would need to clean that directory out once in a while. That would create about 43,200 files per month. –  Dennis Williamson May 18 '10 at 15:45
    
Thanks for fixing my post Dennis. You're right about the clean up, but I just got lazy at the end and just wanted to provide a general approach. Devils in the detail. –  wag2639 May 18 '10 at 20:57
    
Hmm... What about creating the file at startup with "touch /var/downtown_logs/date +%s" and store the path in an enviorment variable. Then update it's timestamp every minute with "touch $enVarName". Then you have a file for each time the computer booted up, and it's timestap tells you when it shut down. –  GummiV Aug 26 '10 at 19:29
    
Have to be careful about the % char in a cron entry. Usually escaping it with a backslash works –  glenn jackman May 16 '11 at 19:09

There's downtimed for this purpose.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.