summaryrefslogtreecommitdiffstats
path: root/scripts/sysinfo_dg.pl
blob: 360a9165f14cd02dfaabafb20ffe5311e21fcdcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/perl
use strict;
use Irssi 20011210.0250 ();
use vars qw($VERSION %IRSSI);
$VERSION = "1.3";
%IRSSI = (
    authors     => 'David Leadbeater',
    contact     => 'dgl@dgl.cx',
    name        => 'sysinfo-dg',
    description => 'Adds a /sysinfo command which prints system information (linux only).',
    license     => 'GNU GPLv2 or later',
    url         => 'http://irssi.dgl.cx/',
);

#This script is mostly my own work but some ideas where taken from /sinfo by
#Laurens Buhler and Alain van Acker. Please leave this credit in the script and
#if you edit it and think the change is worthwhile tell me and i may add it into
#the script and credit you

use vars qw/$colour $graphs $graphs2 $colour2 $style/;
Irssi::command_bind("sysinfo","sysinfo");

sub sysinfo{
   my @options = split(/ /,$_[0]);
   my %info;
   my($hostname,$uname,$procs) = basicinfo();
   my($distro) = distro();
   my($uptime,$users,$loadavg) = uptime();
   my($memsize,$memfree) = meminfo();
   my($swapsize,$swapfree) = swapinfo();
   my($cpumodel,$cpumhz,$cpucache,$bogomips) = cpuinfo();
   my %netinfo = netinfo();
   my($disktotal,$diskused,$hddtype) = df();
   my($videocard,$ethernet) = pciinfo();
   my($screenres,$screendepth);
   ($screenres,$screendepth) = screenres() if $ENV{DISPLAY};

   ($colour,$graphs,$graphs2,$colour2,$style) = parseoptions(\%netinfo,@options);

   %info = (   
      'os' => "$uname - $distro",
      'up' => $uptime,
      'cpu' => "$cpumodel, $cpumhz MHz ($bogomips bogomips)",
      'cache' => $cpucache,
      'mem' => ($memsize-$memfree) . "/$memsize MB (" . percent(($memsize-$memfree),$memsize) . ")",
      'host' => $hostname,
      'users' => $users,
      'load' => $loadavg,
      'procs' => $procs,
      'swap' => ($swapsize-$swapfree) . "/$swapsize MB (" . percent(($swapsize-$swapfree),$swapsize) . ")",
      'disk' => "$diskused/$disktotal MB (" . percent($diskused,$disktotal) . ") ($hddtype)",
      'video' => "$videocard at $screenres ($screendepth bits)",
      'ethernet' => $ethernet,
   );

   for(keys %netinfo){
      $info{$_} = "in: $netinfo{$_}{in} MB, out: $netinfo{$_}{out} MB";
   }

   my $tmp;
   for(split(/ /,$style)){
      $tmp .= ircbit($_,$info{$_}) . " ";
   }
   $tmp =~ s/ $//;
   Irssi::active_win()->command('say ' . $tmp);
   ($colour,$graphs,$graphs2,$colour2,$style) = undef;
}

sub parseoptions{
   my($netinfo,@options) = @_;

   my $tmp = shift(@options) if $options[0] =~ /^\-/;
   $tmp =~ s/^\-//;
   for(split //,$tmp){
	  if($_ eq "c"){
		 $tmp =~ /c(\d+)/;
		 $colour = $1;
		 if(!$colour){
			$colour = 3;
		 }
	  }elsif($_ eq "g"){
		 $tmp =~ /g(\d+)/;
		 $graphs = $1;
		 if(!$graphs){
			$graphs = 9;
		 }
	  }elsif($_ eq "G"){
		 $tmp =~ /G(\d+)/;
		 $graphs2 = $1;
	  }elsif($_ eq "C"){
		 $tmp =~ /C(\d+)/;
		 $colour2 = $1;
	  }
   }
   if(!defined $colour2 && $colour){
	  $colour2 = 15;
   }
   if(defined $graphs && !defined $graphs2){
	  $graphs2 = 3;
   }

# We got the names on the command line
   if($options[1]){
      $style = join(" ",@options);
# style name
   }elsif($options[0]){
      if($options[0] eq "std"){
	     $style = "os up cpu mem video";
      }elsif($options[0] eq "bigger"){
	     $style = "os up cpu cache mem load procs disk video";
      }elsif($options[0] eq "full"){
	     $style = "host os up cpu cache mem users load procs swap disk video ethernet ".join(" ",keys %{$netinfo});
      }elsif($options[0] eq "net"){
	     $style = join(" ",keys %{$netinfo});
      }elsif($options[0] eq "uptime"){
	     $style = "os up";
      }elsif($options[0] eq "use"){
	     $style = "mem swap disk";
      }
   }else{
# no input - default
      $style = "os up cpu mem video";
   }
   
   return($colour,$graphs,$graphs2,$colour2,$style);
}

sub ircbit{
   my($name,$text) = @_;
   $name = " " . $name if $name =~ /^\d/;
   $text = " " . $text if $text =~ /^\d/;
   if($colour){
	  return "$colour$name$colour2\[$text$colour2\]";
   }else{
      return "$name\[$text\]";
   }
}

sub percent{
   my $percent = ($_[1] != 0) ? sprintf("%.1f",(($_[0]/$_[1])*100)) : 0;
   if($graphs){
	  my $tmp = "[";
	  for(1..10){
	     if($_ > sprintf("%.0f",$percent / 10)){
			$tmp .= "-" if !defined $colour;
			$tmp .= "$graphs2-" if defined $colour;
	     }else{
		    $tmp .= "|" if !defined $colour;
		    $tmp .= "$graphs|" if defined $colour;
	     }
	  }
	  $tmp .= "]";
	  return $percent."% ".$tmp;
   }
   return $percent."%";
}

sub uptime{
   my $uptimeinfo = `uptime`;
   if ($uptimeinfo =~ /^\s+(\d+:\d+\w+|\d+:\d+:\d+)\s+up\s+(\d+)\s+day.?\W\s+(\d+):(\d+)\W\s+(\d+)\s+\w+\W\s+\w+\s+\w+\W\s+(\d+).(\d+)/igx) {
     return("$2 days, $3 hours, $4 minutes", $5, "$6.$7");
   }elsif ($uptimeinfo =~ /^\s+(\d+:\d+\w+|\d+:\d+:\d+)\s+up+\s+(\d+):(\d+)\W\s+(\d+)\s+\w+\W\s+\w+\s+\w+\W\s+(\d+).(\d+)/igx) {
	  return("$2 hours, $3 minutes", $4, "$5.$6");
   }elsif ($uptimeinfo =~ /^\s+(\d+:\d+\w+|\d+:\d+:\d+)\s+up\s+(\d+)\s+day.?\W\s+(\d+)\s+min\W\s+(\d+)\s+\w+\W\s+\w+\s+\w+\W\s+(\d+).(\d+)/igx) {
	  return("$2 days, $3 minutes", $4, "$5.$6");
   }elsif ($uptimeinfo =~ /^\s+(\d+:\d+\w+|\d+:\d+:\d+)\s+up+\s+(\d+)\s+min\W\s+(\d+)\s+\w+\W\s+\w+\s+\w+\W\s+(\d+).(\d+)/igx) {
	  return("$2 minutes", $3, "$4.$5");
   }
   return undef;
}

sub meminfo{
   my($memsize,$memfree);
   open(MEMINFO, "<", "/proc/meminfo") or return undef;
   while(<MEMINFO>){
      chomp;
      if(/^MemTotal:\s+(\d+)/){
	     $memsize = sprintf("%.2f",$1/1024);
      }elsif(/^MemFree:\s+(\d+)/){
	     $memfree = sprintf("%.2f",$1/1024);
      }
   }
   close(MEMINFO);
   return($memsize,$memfree);
}

sub swapinfo{
   my($swapsize,$swapused);
   open(SWAPINFO, "<", "/proc/swaps");
   while(<SWAPINFO>){
	  chomp;
	  next if !/^\//;
	  /\S+\s+\S+\s+(\S+)\s+(\S+)/;
	  $swapsize += $1;
	  $swapused += $2;
   }
   close(SWAPINFO);
   my $swapfree =  sprintf("%.2f",($swapsize - $swapused) / 1024);
   $swapsize = sprintf("%.2f", $swapsize / 1024);
   return($swapsize,$swapfree);
}

sub netinfo{
   my(%netinfo);
   open(NETINFO, "<", "/proc/net/dev") or return undef;
   while(<NETINFO>){
	  chomp;
	  next if /^(\s+)?(Inter|face|lo)/;
	  /^\s*(\w+):\s*(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)\s+/;
	  $netinfo{$1}{in} = sprintf("%.2f",$2 / 1048576);
	  $netinfo{$1}{out} = sprintf("%.2f",$3 / 1048576);
   }
   close(NETINFO);
   return %netinfo;
}

sub distro{
   my $distro;
   if(-f "/etc/coas"){
      $distro = firstline("/etc/coas");
   }elsif(-f "/etc/environment.corel"){
      $distro = firstline("/etc/environment.corel");
   }elsif(-f "/etc/debian_version"){
      $distro = "Debian ".firstline("/etc/debian_version");
   }elsif(-f "/etc/mandrake-release"){
      $distro = firstline("/etc/mandrake-release");
   }elsif(-f "/etc/SuSE-release"){
      $distro = firstline("/etc/SuSE-release");
   }elsif(-f "/etc/turbolinux-release"){
      $distro = firstline("/etc/turbolinux-release");
   }elsif(-f "/etc/slackware-release"){
      $distro = firstline("/etc/slackware-release");
   }elsif(-f "/etc/redhat-release"){
      $distro = firstline("/etc/redhat-release");
   }
   return $distro;
}

sub df{
   my($disktotal,$diskused,$mainhd);
   for(`df`){
      chomp;
      next if !/^\/dev\/\S+/;
      next if /(cd|cdrom|fd|floppy)/;
      /^(\S+)\s+(\S+)\s+(\S+)/;
	  $mainhd = $1 if !defined $mainhd;
	  next if not defined $1 or not defined $2;
      $disktotal += $2;
      $diskused += $3;
   }
   $disktotal = sprintf("%.2f",$disktotal / 1024);
   $diskused = sprintf("%.2f",$diskused / 1024);

   $mainhd =~ s/\/dev\/([a-z]+)\d+/$1/;
   my $hddtype = firstline("/proc/ide/$mainhd/model");

   return($disktotal,$diskused,$hddtype);
}

sub basicinfo{
   my($hostname,$sysinfo,$procs);
   chomp($hostname = `hostname`);
   chomp($sysinfo = `uname -sr`);
   opendir(PROC, "/proc");
   $procs = scalar grep(/^\d/,readdir PROC);
   return($hostname,$sysinfo,$procs);
}

sub cpuinfo{
   my($cpumodel,$cpusmp,$cpumhz,$cpucache,$bogomips);
   open(CPUINFO, "<", "/proc/cpuinfo") or return undef;
   while(<CPUINFO>){
      if(/^model name\s+\:\s+(.*?)$/){
	     if(defined $cpumodel){
		    if(defined $cpusmp){
			   $cpusmp++;
		    }else{
		       $cpusmp=2;
		    }
	     }else{
	        $cpumodel = $1;
	     }
      }elsif(/^cpu MHz\s+:\s+([\d\.]*)/){
	     $cpumhz = $1;
      }elsif(/^cache size\s+:\s+(.*)/){
	     $cpucache = $1;
      }elsif(/^bogomips\s+:\s+([\d\.]*)/){
		 $bogomips += $1;
	  }
   }
   $cpumodel .= " SMP ($cpusmp processors)" if defined $cpusmp;
   return($cpumodel,$cpumhz,$cpucache,$bogomips);
}

sub pciinfo{
   my($videocard,$ethernet);
   open(PCI, "-|", "/sbin/lspci") or return undef;
   while(<PCI>){
      chomp;
      if(/VGA compatible controller: (.*?)$/){
         $videocard .= "${1}+ ";
      }elsif(/(Ethernet|Network) controller: (.*?)$/){
	     $ethernet = $1;
      }
   }
   close(PCI);
   $videocard =~ s/\+ $//;
   return($videocard,$ethernet);
}

sub screenres{
   my ($res,$depth);
   for(`xdpyinfo`){
	  if(/\s+dimensions:\s+(\S+)/){
		 $res = $1;
	  }elsif(/\s+depth:\s+(\S+)/){
		 $depth = $1;
	  }
   }
   return($res,$depth);
}

sub firstline{
   my $file = shift;
   open(FILE, "<", $file) or return undef;
   chomp(my $line = <FILE>);
   close(FILE);
   return $line;
}