Uptime and battery state log

Here I go, posting two days in a row.

I got one of those new-fangled Asus Eee PCs recently (with Xandros and not the other thing, obviously) and, being the paranoid freak that I am, I needed to check out how long the battery lasts. And I needed to do that every single time I used it.

So I mashed up a little script for it, and then started adding functionality to it with almost every run, and it sort of grew. So I recently added the traditional getopt stuff, and a license and some basic error checking, and decided to post it.

It needs acpi and uptime to run at all, and if you want to print stuff out to stdout as well, then you need tee.

It should be pretty straightforward to use, but here's an overview anyway.

First, you gotta make sure that the directory for the logs already exists, or that the script will be able to create it on its own. Personally, I do the first one. The default directory is /var/log/uptimed, so you would go:

sudo mkdir -p /var/log/uptimed
sudo chmod a+rw /var/log/uptimed


And basically, you're all set to go. Now you can run it by simply going:

./uptimed &


Or you can use one of the many available options, like adding a prefix, a suffix and printing the output to screen as well as the log file:

./uptimed -s -c "some suffix" -p "some prefix" &


Enough rambling, here's the code.
1  #!/bin/bash
2  #
3  # uptimed (uptime daemon)
4  #
5  # Checks uptime and some acpi settings from time to time and logs 
6  # that information in a specified file. It's designed to be run often.
7  #
8  # Parameters:
9  #   -t | --sleep-time   time between checks (default: 6 minutes)
10 #   -s | --stdout       write info to stdout as well as the file
11 #   -f | --file         write to a log file with a name like this one 
12 #   -e | --extension    specify an extensionto the log file
13 #   -u | --no-uptime    DON'T log current uptime
14 #   -b | --no-battery   DON'T log batery charge
15 #   -d | --no-thermal   DON'T log current temperature
16 #   -a | --all          log uptime, battery and thermal (default)
17 #   -c | --comment      append a comment to each line (default: empty)
18 #   -p | --prefix       prepend a comment to each line (default: empty)
19 
20 # Requires:
21 #   uptime
22 #   acpi
23 #   tee
24 #
25 # Author:
26 #   Konrad Siek <konrad.siek@gmail.com>
27 #
28 # License information:
29 #
30 # This program is free software: you can redistribute it and/or modify
31 # it under the terms of the GNU General Public License as published by
32 # the Free Software Foundation, either version 3 of the License, or
33 # (at your option) any later version.
34 #
35 # This program is distributed in the hope that it will be useful,
36 # but WITHOUT ANY WARRANTY; without even the implied warranty of
37 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38 # GNU General Public License for more details.
39 #
40 # You should have received a copy of the GNU General Public License
41 # along with this program. If not, see <http://www.gnu.org/licenses/>.
42 #
43 # Copyright 2009 Konrad Siek
44 
45 # Presets
46 sleep_time=360                    
47 echo_stdin=false #true
48 filename=/var/log/uptimed/uptime
49 extension=`date +%Y%m%d`
50 uptime_on=true
51 battery_on=true
52 thermal_on=true
53 comment=""
54 
55 # Retrieve program parameters
56 options=$( \
57 getopt -o t:s::f:e:ubdac:p: \
58 --long sleep-time:,stdout::,file:,extension:,\
59 no-uptime,no-battery,no-thermal,all,comment:,prefix \
60 -n $0 -- "$@" \
61 )
62 
63 # Check if getopts worked as it should
64 [ $? != 0 ] &&
65     exit 1
66 
67 # Iterate over parameters and set up the program
68 eval set -- "$options"
69 while true
70 do
71     case "$1" in
72         -t|--sleep-time)
73             sleep_time=$2
74             shift 2
75         ;;
76         -f|--file)
77             filename=$2
78             shift 2
79         ;;
80         -e|--extension)
81             extension=$2
82             shift 2
83         ;;
84         -s|--stdout)
85             if [ "$2" != "" ]
86             then 
87                 echo_stdin=$2
88             else
89                 echo_stdin=true                
90             fi
91             shift 2
92         ;;
93         -u|--no-uptime)
94             uptime_on=$false
95             shift 1
96         ;;
97         -b|--no-battery)
98             battery_on=$false
99             shift 1
100        ;;
101        -d|--no-thermal)
102            thermal_on=$false
103            shift 1
104        ;;
105        -c|--comment)
106            comment="$2"
107            shift 2
108        ;;
109        -p|--prefix)
110            prefix="[$2] "
111            shift 2
112        ;;
113        -a|--all)
114            uptime_on=true
115            battery_on=true
116            thermal_on=true
117            shift 1
118        ;;
119        --) 
120            shift 
121            break 
122        ;;
123        *)
124            echo "This is, of course, impossible... Milliways?" >& 2
125            exit -1 
126        ;;
127    esac
128done
129
130# Create the base directory,
131dir=`dirname "$filename.$extension"`
132if [ ! \( -d "$dir" \) ] 
133then
134    mkdir -p "$dir"
135    if [ $? != 0 ]
136    then
137        echo "$0:Directory does not exist and can't be created: $dir" >& 2
138        exit 1 
139    fi
140fi
141
142# Create the log file. 
143if [ ! \( -e "$filename.$extension" \) ] 
144then
145    touch "$filename.$extension"
146    if [ $? != 0 ]
147    then
148        echo "$0:File does not exist and can't be created: $dir" >& 2
149        exit 1 
150    fi
151fi
152
153# The main loop thing.
154while true
155do    
156    # Gather information.
157    [ $uptime_on ] && \
158        uptime=`uptime | tr -s ' ' | cut -f 1 --complement -d ' '`
159    [ $battery_on ] && \
160        battery=`acpi -b | tr -s ' ' | cut -f 2,5,6,7 -d ' '`
161    [ $thermal_on ] && \
162        thermal=`acpi -tB | tr -s ' ' | cut -f 4,5,7 -d ' ' | tr -d ','`
163
164    # Compose the message.
165    message=""
166    [ -n "$uptime" ] && \
167        message="$message$uptime "
168    [ -n "$battery" ] && \
169        message="$message$battery "
170    [ -n "$thermal" ] && \
171        message="$message$thermal "
172    [ -n "$comment" ] && \
173        message="$message#$comment"
174    [ -n "$prefix" ] && \
175        message="$prefix$message"
176
177    # Send message to file, etc.
178    [ $echo_stdin == true ] && \
179        echo -e $message | tee -a "$filename.$extension" || \
180        echo -e $message >> "$filename.$extension"
181
182    # Wait and repeat.
183    sleep $sleep_time
184done
185
186


The code is also available at GitHub as bash/uptimed.

lala moulati ana9a maghribia

seo

 

Blogroll

Site Info

Text

telechargementz Copyright © 2009 WoodMag is Designed by Ipietoon for Free Blogger Template