2 # we register the script
3 # if someone knows how to unload it clean....do tell
4 IRC
::register
("xas", "1.9", "","XChat aMule stats");
6 IRC
::print "\n\0033 Follow the \0034 white\0033 rabbit\0038...\003\n";
7 IRC
::print "\n\0035 Use command \0038/xas\0035 to print out aMule statistics\003";
8 # we have no life. we are robots...and we hang around in here:
9 IRC
::print "\0035 (#amule @ irc.freenode.net)\003";
11 IRC
::add_command_handler
("xas","xas");
13 #25.06.2005 - PleuR : int_to_byte, ratio-fixer, fixed line 1 (is connected to online ?)
14 #16.12.2005 - stefanero : some kad cleanups
15 #12.12.2005 - fulgas ; made kad work with xas
16 #06.05.2005 - niet : file handle change
17 #12.10.2004 - bisley : added session/total ratios
18 #16.06.2004 - niet : added support for memory usage and binary name
19 #05.05.2004 - Jacobo221 : fixed typos, sig 2 support, new outputs, crash detect
20 #29.04.2004 - niet : renamed astats to xas (X-Chat Amule statistics)
21 #22.04.2004 - citroklar : added smp support
22 # 2004 - bootstrap : some hints on file opening
23 # 2004 - niet : used some of cheetah.pl script and so astats was born
25 # Five status currently: online, connecting, offline, closed, crashed
29 chomp(my $amulename = `ps --no-header -u $ENV{USER} -o ucmd --sort start_time|grep amule|head -n 1`);
30 #amule binary date (because we still don't have date in CVS version). GRRRRRR
31 #ls -lAF `ps --no-header -u j -o cmd --sort start_time|grep amule|head -n 1|awk '{print $2}'`
33 chomp(my $uptime = `uptime|cut -d " " -f 4- | tr -s " "`);
34 # number of cpu's calculated from /proc/cpuinfo
35 chomp(my $number_cpus = `cat /proc/cpuinfo | grep 'processor' -c`);
37 chomp(my $cpu = `cat /proc/cpuinfo | grep 'model name' -m 1 | cut -f 2 -d ":" | cut -c 2-`);
39 chomp(my $mhz = `cat /proc/cpuinfo | grep "cpu MHz" -m 1 | cut -f 2 -d ":" | cut -c 2-`);
40 # what is the aMule's load on cpu
41 chomp(my $amulecpu = `ps --no-header -C $amulename -o %cpu --sort start_time|head -n 1`);
42 # how much memory is aMule using
43 chomp(my $amulemem = (sprintf("%.02f", `ps --no-header -C $amulename -o rss --sort start_time|head -n 1` / 1024 )));
46 # there is no spoon...err.... signature
47 open(AMULESIGFILE
,"$ENV{'HOME'}/.aMule/amulesig.dat") or die "aMule online signature not found. Did you enable it ?";
48 chomp(@amulesigdata = <AMULESIGFILE
>);
51 # are we high or what ? :-Q
52 if ($amulesigdata[4] eq "H")
57 # are we online / offline / connecting
59 if($amulesigdata[5]==2) {
60 if ($amulesigdata[0]==0) {
61 #$amulestatus="Not Connected";
62 $amulextatus="Kad: ok";
63 } elsif ($amulesigdata[0]==2) { # Since aMule v2-rc4
64 $amulestatus="connecting";
65 $amulextatus="| Kad: ok";
67 $amulestatus="online";
68 $amulextatus="with $amuleid ID on server $amulesigdata[1] [ $amulesigdata[2]:$amulesigdata[3] ] | Kad: ok";
70 } elsif ($amulesigdata[5]==1) {
71 if ($amulesigdata[0]==0) {
72 #$amulestatus="Not Connected";
73 $amulextatus="Kad: firewalled";
74 } elsif ($amulesigdata[0]==2) { # Since aMule v2-rc4
75 $amulestatus="connecting";
76 $amulextatus="| Kad: firewalled";
78 $amulestatus="online";
79 $amulextatus="with $amuleid ID on server $amulesigdata[1] [ $amulesigdata[2]:$amulesigdata[3] ] | Kad: firewalled";
82 if ($amulesigdata[0]==0) {
83 $amulestatus="Not Connected";
84 $amulextatus="| Kad: off";
85 } elsif ($amulesigdata[0]==2) { # Since aMule v2-rc4
86 $amulestatus="connecting";
87 $amulextatus="| Kad: off" ;
89 $amulestatus="online";
90 $amulextatus="with $amuleid ID on server $amulesigdata[1] [ $amulesigdata[2]:$amulesigdata[3] ] | Kad: off";
94 # total download traffic in bytes (int_to_byte *after* calculations)
95 my $tdl = $amulesigdata[11];
97 # total upload traffic in bytes
98 my $tul = $amulesigdata[12];
100 # session download traffic in bytes
101 my $sdl = $amulesigdata[14];
103 # session upload traffic in bytes
104 my $sul = $amulesigdata[15];
107 my $totalratio = calc_ratio
($tdl,$tul);
109 my $sessionratio = calc_ratio
($sdl,$sul);
111 # do int_to_bytes to make human-readable output
112 $tdl = int_to_bytes
($tdl);
113 $tul = int_to_bytes
($tul);
114 $sdl = int_to_bytes
($sdl);
115 $sul = int_to_bytes
($sul);
117 # convert runtime from sec to string
118 my $seconds = $amulesigdata[16];
119 my $days = pull_count
($seconds, 86400);
120 my $hours = pull_count
($seconds, 3600);
121 my $minutes = pull_count
($seconds, 60);
126 $runtime = sprintf "%02iD %02ih %02imin %02is", $days, $hours, $minutes, $seconds;
129 $runtime = sprintf "%02ih %02imin %02is", $hours, $minutes, $seconds;
131 elsif ($minutes > 0) {
132 $runtime = sprintf "%02imin %02is", $minutes, $seconds;
135 $runtime = sprintf "%02is", $seconds;
140 # if current user isn't running aMule
141 if ( ! `ps --no-header -u $ENV{USER} | grep amule`) {
142 IRC
::command
"/say $amulesigdata[10] is not running";
143 # Crash detection is implemented since v2-rc4, so XAS should be backwards compatible
144 if ( grep(/^1./,$amulesigdata[13]) || $amulesigdata[13]=="2.0.0rc1" || $amulesigdata[13]=="2.0.0rc2" || $amulesigdata[13]=="2.0.0rc3" ) {
145 IRC
::command
"/say aMule $amulesigdata[13] was closed after $runtime!" }
146 elsif ( ! grep(/^00 /,$runtime)) {
147 IRC
::command
"/say aMule $amulesigdata[13] crashed after $runtime!" }
149 IRC
::command
"/say aMule $amulesigdata[13] was closed" };
150 IRC
::command
"/say Total download traffic: $tdl";
151 IRC
::command
"/say Total upload traffic: $tul" }
152 # if aMule is running
154 if ($amulesigdata[0]==0 && $amulesigdata[5]==0){
155 IRC
::command
"/say $amulesigdata[10] is not connected";
158 IRC
::command
"/say $amulesigdata[10] is $amulestatus $amulextatus";}
160 IRC
::command
"/say aMule $amulesigdata[13] is using $amulecpu% CPU, $amulemem MB of memory and it has been running for $runtime";
162 # we only display "number of cpus" when we have more then one
163 if ($number_cpus > 1) {
164 IRC
::command
"/say on $number_cpus x $cpu @ $mhz up $uptime" }
166 IRC
::command
"/say on $cpu @ $mhz MHz up $uptime" };
168 IRC
::command
"/say Sharing $amulesigdata[9] files with $amulesigdata[8] clients in queue";
169 IRC
::command
"/say Total download traffic: $tdl, total upload traffic: $tul, Total Ratio: $totalratio";
170 IRC
::command
"/say Session download traffic: $sdl, session upload traffic: $sul, Session Ratio: $sessionratio";
171 IRC
::command
"/say Current DL speed: $amulesigdata[6] KB/s, current UL speed: $amulesigdata[7] KB/s" };
176 # usage: $count = pull_count(seconds, amount)
177 # remove from seconds the amount quantity, altering caller's version.
178 # return the integral number of those amounts so removed.
180 my($answer) = int($_[0] / $_[1]);
181 $_[0] -= $answer * $_[1];
189 # total download traffic in bytes
192 # total upload traffic in bytes
199 if ($ul > 0) { # we don't want 'division by zero'-error
200 $ratiodown = ($dl/$ul);
204 if ($ratiodown == 0) { # we don't want 1:0 ratio's if down eq 0
207 elsif ($ratiodown < 0.5) {
208 if ($ratiomethod == 1) { # Set down to 1, calculate up
209 $ratioup = ($ul/$dl);
212 if ($ratiomethod == 2) { # Increase by 10 until greater than 0.5
213 while ($ratiodown < 0.5) {
214 $ratiodown = $ratiodown * 10;
215 $ratioup = $ratioup * 10;
219 $ratiodown = (sprintf("%0.1f",$ratiodown));
220 $ratioup = (sprintf("%0.1f",$ratioup));
222 return "$ratioup:$ratiodown";
227 my($value) = int($_[0]);
229 if ($value >= 1073741824) {
230 $value = (sprintf("%0.2f",$value/1073741824));
233 elsif ($value >= 1048576) {
234 $value = (sprintf("%0.2f",$value/1048576));
237 elsif ($value >= 1024) {
238 $value = (sprintf("%0.2f",$value/1024));
242 $value = (sprintf("%0.2f",$value));