initial checkin
[grml-etc.git] / etc / skel / .wmi / sti.pl
1 #!/usr/bin/perl
2
3 # STI - StatusText Improved ;) 
4 # A simple program to update the bartext in WMI
5 # last modified: 20 June 2004
6
7 # Copyright (C) 2004 Nicholas Lativy
8
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23 # To use this program put it in a directory in your $PATH 
24 # make executable and put the following lines in your ~/.xinitrc file:
25 #  sti &
26 #  wmi
27
28 use strict;
29
30 # The 4 following variables should be set by the user
31 #--------------------------------------------------
32 # my $TFORMAT = "%a %d %B - %H%M"; # time format (see man date)
33 #-------------------------------------------------- 
34 my $TFORMAT = "%D - %H:%M";
35 my $MAILDIR = "~/mail/inbox";   # location of user maildir
36 #my $TOTALMEM = 256;              # physical memory in MB
37 my $TOTALMEM = `free -mt | grep Mem | cut -b16-20`;
38 my $RTIME = 5;                   # time between each refresh of stats
39 # note that the actual time between each refresh will be at least 1/2
40 # a second longer than $RTIME
41
42 for (;;) {
43    my ($mail, $new) = &check_mail($MAILDIR);
44    my ($mem_used, $cpu_used) = &sys_stats($TOTALMEM);
45    my $time = &get_time($TFORMAT);
46    system "wmiremote -t \"[$cpu_used% MEM: $mem_used%]  [$mail mail, $new new]  [$time]\"";
47    #system "wmiremote -t \"[CPU: $cpu_used% MEM: $mem_used%]  [$mail mail, $new new]  [$time]\"";
48    #print "[CPU: $cpu_used% MEM: $mem_used%]  [$mail mail, $new new]  [$time]\n"; # this line was for testing
49    sleep $RTIME;
50 }
51
52 sub check_mail {
53    # check the user's email
54    # currently only supports maildir
55    my ($mail) = @_;
56    chomp(my $cur = `ls $MAILDIR/cur | wc -l`);
57    chomp(my $new = `ls $MAILDIR/new | wc -l`);
58    return($cur + $new, $new);
59 }
60
61 sub get_time {
62    # get the localtime, currently using the date
63    # command. change it to use perl's localtime?
64    my ($format) = @_;
65    chomp(my $date = `date "+${TFORMAT}"`);
66    return($date);
67 }
68
69 sub sys_stats {
70    # get % of CPU and memory used
71    my ($mem) = @_;
72    chomp(my $mem_free = `free -mt | grep Mem | cut -b38-40`);
73    chomp(my $cpu_idle = `uptime`);
74 #   chomp(my $cpu_idle = `top -bn2d0.5 | grep Cpu | tail -n1`);
75 #   $cpu_idle =~ /(\d+\.\d+)%(\s)idle/;
76 #   $cpu_idle = $1;
77 #   my $cpu_used = 100 - $cpu_idle;
78    my $cpu_used = $cpu_idle;
79    my $mem_used = 100 - $mem_free / $mem * 100;
80    # return rounded up figures
81    return (sprintf("%.0f",$mem_used), sprintf($cpu_used));
82 }