Fix language getting reset to system default on saving preferences
[amule.git] / src / utils / xas / xas.pl
blob5f657e7655decef9f1ec78e9e5d2f4cc48e72bd4
1 #!/usr/bin/perl
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");
5 # welcome message
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";
10 # command that we use
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
26 sub xas
28 #amule program name
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}'`
32 # system uptime
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`);
36 # type of cpu
37 chomp(my $cpu = `cat /proc/cpuinfo | grep 'model name' -m 1 | cut -f 2 -d ":" | cut -c 2-`);
38 # cpu speed
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 )));
45 # bootstrap
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>);
49 close AMULESIGFILE;
51 # are we high or what ? :-Q
52 if ($amulesigdata[4] eq "H")
53 {$amuleid="high"}
54 else
55 {$amuleid="low"};
57 # are we online / offline / connecting
58 #kad on
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";
66 } else {
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";
77 } else {
78 $amulestatus="online";
79 $amulextatus="with $amuleid ID on server $amulesigdata[1] [ $amulesigdata[2]:$amulesigdata[3] ] | Kad: firewalled";
81 } else {
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" ;
88 } else {
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];
106 # ratio
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);
123 my $runtime;
125 if ($days > 0) {
126 $runtime = sprintf "%02iD %02ih %02imin %02is", $days, $hours, $minutes, $seconds;
128 elsif ($hours > 0) {
129 $runtime = sprintf "%02ih %02imin %02is", $hours, $minutes, $seconds;
131 elsif ($minutes > 0) {
132 $runtime = sprintf "%02imin %02is", $minutes, $seconds;
134 else {
135 $runtime = sprintf "%02is", $seconds;
138 # and display it
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!" }
148 else {
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
153 else {
154 if ($amulesigdata[0]==0 && $amulesigdata[5]==0){
155 IRC::command "/say $amulesigdata[10] is not connected";
157 else {
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" }
165 else {
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" };
172 return 1;
173 # that's it
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.
179 sub pull_count {
180 my($answer) = int($_[0] / $_[1]);
181 $_[0] -= $answer * $_[1];
182 return $answer;
185 sub calc_ratio
187 my $ratiomethod = 1;
189 # total download traffic in bytes
190 my $dl = $_[0];
192 # total upload traffic in bytes
193 my $ul = $_[1];
195 my $ratiodown = 0;
196 my $ratioup = 0;
198 # calculate ratio
199 if ($ul > 0) { # we don't want 'division by zero'-error
200 $ratiodown = ($dl/$ul);
201 $ratioup = 1;
204 if ($ratiodown == 0) { # we don't want 1:0 ratio's if down eq 0
205 $ratioup = 0;
207 elsif ($ratiodown < 0.5) {
208 if ($ratiomethod == 1) { # Set down to 1, calculate up
209 $ratioup = ($ul/$dl);
210 $ratiodown = 1;
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";
225 sub int_to_bytes
227 my($value) = int($_[0]);
229 if ($value >= 1073741824) {
230 $value = (sprintf("%0.2f",$value/1073741824));
231 return "$value GiB";
233 elsif ($value >= 1048576) {
234 $value = (sprintf("%0.2f",$value/1048576));
235 return "$value MiB";
237 elsif ($value >= 1024) {
238 $value = (sprintf("%0.2f",$value/1024));
239 return "$value KiB";
241 else {
242 $value = (sprintf("%0.2f",$value));
243 return "$value B";