Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 23.34 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (c) 2002-2005 David Rudie
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. #    notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions and the following disclaimer in the
  13. #    documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. #
  27. # Several additions and fixes to this script were contributed by Travis
  28. # Morgan and therefore are Copyright (c) 2003-2006 Travis Morgan
  29. #
  30. # If you notice any bugs including spacing issues, wrong detection of hardware,
  31. # obvious features missing, etc, we both want to hear about them.  If you make
  32. # this script work on other operating systems and/or architectures please send
  33. # either of us your patches.  My e-mail address is d.rudie@gmail.com and
  34. # Travis Morgan's e-mail address is imbezol@criticaldamage.com
  35. #
  36. # The latest version can be obtained from either http://www.inexistent.com/ or
  37. # http://imbezol.org/sysinfo/
  38. #
  39. # You can also reach Travis (imbezol) in #crd on efnet.
  40.  
  41. use POSIX qw(floor);
  42. use strict;
  43.  
  44.  
  45. # Set up the arrays and variables first.
  46. use vars qw(
  47.     @arr
  48.     @arr1
  49.     @arr2
  50.     $cpu
  51.     @cpu
  52.     @cpuinfo
  53.     $data
  54.     $distro
  55.     $distrov
  56.     @distros
  57.     @data
  58.     $df
  59.     @dmesgboot
  60.     @hinv
  61.     @meminfo
  62.     $mhz
  63.     @mhz
  64.     $model
  65.     @netdev
  66.     @netstat
  67.     @nic
  68.     @nicname
  69.     $realdistro
  70.     $smp
  71.     @smp
  72.     $stream
  73.     $sysctl
  74.     @uptime
  75.     $var
  76.     $vara
  77.     $varb
  78.     $varc
  79.     $vard
  80.     $varh
  81.     $varm
  82.     $varp
  83.     $varx
  84.     $vary
  85.     $varz
  86.     $col1
  87.     $col2
  88. );
  89.  
  90.  
  91. # Specify your NIC interface name (eth0, rl0, fxp0, etc) and a name for it.
  92. #
  93. # Example: @nic      = ('eth0', 'eth1');
  94. #                   @nicname = ('External', 'Internal');
  95. #
  96. # NOTE: If you set one then you HAVE to set the other.
  97. @nic        = ('eth0');
  98. @nicname    = ('Local');
  99.  
  100. $col1 = '';
  101. $col2 = '';
  102.  
  103. # These are the default settings for which information gets displayed.
  104. # 0 = Off; 1 = On
  105. my $useShortHostname        = 0;
  106.  
  107. my $showHostname        = 1;
  108. my $showOS          = 1;
  109. my $showCPU         = 1;
  110. my $showProcesses       = 1;
  111. my $showUptime          = 1;
  112. my $showUsers           = 1;
  113. my $showLoadAverage     = 1;
  114. my $showBattery         = 0;    # Requires APM and /proc/apm or /proc/acpi/battery/
  115. my $showMemoryUsage     = 1;
  116. my $showDiskUsage       = 1;
  117. my $showNetworkTraffic      = 1;
  118. my $showDistro          = 1;
  119.  
  120. ###############################################
  121. ### Nothing below here should need changed. ###
  122. ###############################################
  123.  
  124. my $sysinfoVer  = '2.81.25';
  125. my $sysinfoDate = 'Sep 11, 2015, 10:22 MST';
  126.  
  127. my $os      = `uname -s`; chomp($os);
  128. my $osn     = `uname -n`; chomp($osn);
  129. if($useShortHostname) {
  130.     $osn    =~ s/([^\.]*).*/$1/;
  131. }
  132. my $osv     = `uname -r`; chomp($osv);
  133. my $osm     = `uname -m`; chomp($osm);
  134. my $uname   = "$os $osv/$osm";
  135.  
  136. my $darwin      = 1 if $os =~ /^Darwin$/;
  137. my $freebsd   = 1 if $os =~ /^FreeBSD$/;
  138. my $dragonfly   = 1 if $os =~ /^DragonFly$/;
  139. my $linux       = 1 if $os =~ /^Linux$/;
  140. my $netbsd      = 1 if $os =~ /^NetBSD$/;
  141. my $openbsd     = 1 if $os =~ /^OpenBSD$/;
  142. my $irix        = 1 if $os =~ /^IRIX$/;
  143. my $irix64      = 1 if $os =~ /^IRIX64$/;
  144. my $sun         = 1 if $os =~ /^SunOS$/;
  145.  
  146.  
  147. my $alpha       = 1 if $osm =~ /^alpha$/;
  148. my $arm         = 1 if $osm =~ /^arm$/;
  149. my $armv6l      = 1 if $osm =~ /^armv6l$/;
  150. my $armv7l      = 1 if $osm =~ /^armv7l$/;
  151. my $i586        = 1 if $osm =~ /^i586$/;
  152. my $i686        = 1 if $osm =~ /^i686$/;
  153. my $ia64        = 1 if $osm =~ /^ia64$/;
  154. my $mips        = 1 if $osm =~ /^mips$/;
  155. my $parisc      = 1 if $osm =~ /^parisc$/;
  156. my $parisc64    = 1 if $osm =~ /^parisc64$/;
  157. my $ppc         = 1 if $osm =~ /^ppc$/;
  158. my $ppc64       = 1 if $osm =~ /^ppc64$/;
  159. my $s390        = 1 if $osm =~ /^s390$/;
  160. my $s390x       = 1 if $osm=~ /^s390x$/;
  161. my $sh          = 1 if $osm=~ /^sh/;
  162. my $sparc       = 1 if $osm =~ /^sparc$/;
  163. my $sparc64     = 1 if $osm =~ /^sparc64$/;
  164. my $x86_64      = 1 if $osm =~ /^x86_64$/;
  165. my $amd64       = 1 if $osm =~ /^amd64$/;
  166.  
  167. my $d7      = 1 if $darwin && $osv =~ /^7\.\d+\.\d+/;
  168. my $d8      = 1 if $darwin && $osv =~ /^8\.\d+\.\d+/;
  169. my $d9      = 1 if $darwin && $osv =~ /^9\.\d+\.\d+/;
  170. my $l26     = 1 if $linux && $osv =~ /^2\.6/;
  171. my $l3      = 1 if $linux && $osv =~ /^2\.7/  || $osv =~ /^3\./ || $osv =~ /^4\./;
  172. my $f_old   = 1 if $freebsd && $osv =~ /^4\.1-/ || $osv =~ /^4\.0-/ || $osv =~ /^3/ || $osv =~ /^2/;
  173.  
  174. my $isJail = `sysctl -n security.jail.jailed` if $freebsd;
  175.  
  176. my $progArgs = $ARGV[0];
  177. if($progArgs) {
  178.     if($progArgs eq '-v' || $progArgs eq '--version') {
  179.         print "SysInfo v$sysinfoVer   $sysinfoDate\n";
  180.         print "Written by David Rudie <d.rudie\@gmail.com> and Travis Morgan <imbezol\@criticaldamage.com>\n";
  181.         exit -1;
  182.     }
  183. }
  184.  
  185. if($linux) {
  186.     @cpuinfo        = &openfile("/proc/cpuinfo");
  187.     @meminfo        = &openfile("/proc/meminfo");
  188.     @netdev         = &openfile("/proc/net/dev");
  189.     @uptime         = &openfile("/proc/uptime");
  190.  
  191.     $df         = 'df -lkP';
  192.  
  193.     if($showDistro) {
  194.         @distros = (
  195.         "Arch", "/etc/arch-release",
  196.         "Gentoo",       "/etc/gentoo-release",
  197.         "Fedora Core",  "/etc/fedora-release",
  198.         "SUSE",         "/etc/SuSE-release",
  199.         "Slackware",    "/etc/slackware-version",
  200.         "Cobalt",       "/etc/cobalt-release",
  201.         "Debian",       "/etc/debian_version",
  202.         "Mandrake",     "/etc/mandrake-release",
  203.         "Mandrake",     "/etc/mandrakelinux-release",
  204.         "Yellow Dog",   "/etc/yellowdog-release",
  205.         "OpenFiler",    "/etc/distro-release",
  206.         "Red Hat",      "/etc/redhat-release"
  207.         );
  208.         $distro = "";
  209.         $distrov = "";
  210.         do {
  211.             if (-e $distros[1]) {
  212.                 $distro = $distros[0];
  213.                 $distrov = `cat $distros[1] | head -n 1`;
  214.                 chomp($distrov);
  215.                 $distrov =~ s/[^0-9]*([0-9.]+)[^0-9.]{0,1}.*/$1/;
  216.             }
  217.             shift @distros; shift @distros;
  218.         } until (scalar @distros == 0) || (length $distro > 0);
  219.         if ($distro eq "Debian") {
  220.             if (-e "/etc/issue") {
  221.                 my $issue = `head -n 1 /etc/issue`;
  222.                 chomp($issue);
  223.                 if ($issue =~ /^Raspbian/) {
  224.                     $distro     = "Raspbian";
  225.                     $distrov    = $issue;
  226.                     $distrov    =~ s/.* ([0-9.]*) .*/$1/;
  227.                 } elsif ($issue =~ /^OSMC:/) {
  228.                     $distro     = "Open Source Media Center";
  229.                     $distrov    = "";
  230.                 }
  231.             }
  232.             if (-e "/etc/lsb-release") {
  233.                 $realdistro = `cat /etc/lsb-release | grep DISTRIB_DESCRIPTION`;
  234.                 if ($realdistro ne "") {
  235.                     chomp $realdistro;
  236.                     $realdistro =~ s/DISTRIB_DESCRIPTION="//;
  237.                     $realdistro =~ s/"$//;
  238.                     $distro     = $realdistro;
  239.                     $distrov    = $realdistro;
  240.                     $distro     =~ s/ [0-9.]+.*$//;
  241.                     $distrov    =~ s/$distro //;
  242.                     if ($distro eq $distrov) {
  243.                         $distrov = "";
  244.                     }
  245.                 }
  246.             }
  247.         }
  248.         if ($distro eq "Red Hat") {
  249.             $realdistro = `cat /etc/redhat-release`;
  250.             if ($realdistro =~ "^CentOS") {
  251.                 $distro = "CentOS";
  252.             }
  253.         }
  254.         if ($distro eq "SUSE") {
  255.             $realdistro = `cat /etc/SuSE-release`;
  256.             if ($realdistro =~ "^openSUSE") {
  257.                 $distro = "openSUSE";
  258.             }
  259.             elsif ($realdistro =~ "^SUSE Linux Enterprise Server") {
  260.                 $distro = "SLES";
  261.             }
  262.             elsif ($realdistro =~ "^SUSE Linux Enterprise Desktop") {
  263.                 $distro = "SLED";
  264.             }
  265.         }
  266.     }
  267. } elsif($irix || $irix64) {
  268.     @hinv           = `hinv`;
  269. } else {
  270.     @dmesgboot      = &openfile("/var/run/dmesg.boot");
  271.     if($sun) {
  272.         @netstat        = `netstat -in`;
  273.     } else {
  274.         @netstat        = `netstat -ibn`;
  275.     }
  276.     if($darwin) {
  277.         $sysctl     = '/usr/sbin/sysctl';
  278.     } else {
  279.         $sysctl     = '/sbin/sysctl';
  280.     }
  281.     if($arm) {
  282.         $df         = 'df -k';
  283.     } elsif($f_old) {
  284.         $df         = 'df -k';
  285.     } else {
  286.         $df         = 'df -lk';
  287.     }
  288.  
  289. }
  290.  
  291. if($showCPU) {
  292.     if($freebsd || $dragonfly) {
  293.         if($alpha) {
  294.             @cpu        = grep(/^COMPAQ/, @dmesgboot);
  295.             $cpu        = join("\n", $cpu[0]);
  296.         } else {
  297.             if ($isJail == '1') # full dmesg not available to jails
  298.             {
  299.                 $cpu        = "";
  300.                 @cpu        = `sysctl -n hw.model`;
  301.                 $cpu        = join("\n", @cpu);
  302.             }
  303.             else
  304.             {
  305.                 $cpu        = "";
  306.                 @cpu        = grep(/CPU: /, @dmesgboot);
  307.                 $cpu        = join("\n", @cpu);
  308.                 $cpu        =~ s/Warning:.*disabled//;
  309.                 @cpu        = split(/CPU: /, $cpu);
  310.                 $cpu        = $cpu;
  311.                 $cpu        =~ s/\s\d\.\d\dGHz//g;
  312.                 $cpu        =~ s/\s*[^\s]*-class CPU//gi;
  313.                 $cpu        =~ s/(\S*)-MHz/$1 MHz/gi;
  314.                 $cpu        =~ s/@\s+//;
  315.             }
  316.             chomp($cpu);
  317.             $smp        = `$sysctl -n hw.ncpu`;
  318.             chomp($smp);
  319.         if ( $smp eq 1 ) {
  320.                 $smp = "";
  321.             }
  322.         }
  323.     }
  324.     if($netbsd) {
  325.         if($alpha) {
  326.             @cpu        = grep(/^COMPAQ/, @dmesgboot);
  327.             $cpu        = join("\n", $cpu[0]);
  328.             @cpu        = split(/, /, $cpu);
  329.             $cpu        = $cpu[0];
  330.         } else {
  331.             @cpu        = grep(/^v?cpu0.*MHz/, @dmesgboot);
  332.             @cpu        = grep(!/apic/, @cpu);
  333.             @cpu        = split(/: /,$cpu[0]);
  334.             $cpu        = $cpu[1];
  335.             $cpu        =~ s/, id.*//;
  336.             $mhz        = $cpu;
  337.             $mhz        =~ s/.* ([.0-9]+) *MHz.*$/$1/;
  338.             $cpu        =~ s/,.*//;
  339.             $cpu        = "$cpu ($mhz MHz)";
  340.             $smp        = `$sysctl -n hw.ncpu`;
  341.             chomp($smp);
  342.             if ( $smp eq 1 ) {
  343.                 $smp = "";
  344.             }
  345.         }
  346.     }
  347.     if($openbsd) {
  348.         @cpu        = grep(/cpu0: /, @dmesgboot);
  349.         @cpu        = grep(/[M|G]Hz/, @cpu);
  350.         $cpu        = join("\n", @cpu);
  351.         @cpu        = split(/: /, $cpu);
  352.         $cpu        = $cpu[1];
  353.         $cpu        =~ s/, / /;
  354.         $cpu        =~ s/([0-9.]* [MG]Hz)$/($1)/;
  355.         $smp        = `$sysctl -n hw.ncpu`;
  356.         chomp($smp);
  357.         if ( $smp eq 1 ) {
  358.             $smp = "";
  359.         }
  360.     }
  361.     if($irix || $irix64) {
  362.         @cpu        = grep(/CPU:/, @hinv);
  363.         $cpu        = join("\n", @cpu);
  364.         $cpu        =~ s/^.*(R[0-9]*) .*$/$1/;
  365.         @mhz        = grep(/MHZ/, @hinv);
  366.         $mhz        = join("\n", @mhz);
  367.         $mhz        = $mhz[0];
  368.         $mhz        =~ s/^.* ([0-9]*) MHZ.*$/$1/;
  369.         @smp        = grep(/ IP/, @hinv);
  370.         $smp        = $smp[0];
  371.         $smp        =~ s/^([0-9]*) .*$/$1/;
  372.         chomp($smp);
  373.         chomp($cpu);
  374.         chomp($mhz);
  375.         $cpu        = "MIPS $cpu ($mhz MHz)";
  376.     }
  377.     if($linux) {
  378.         if($alpha) {
  379.             $cpu        = &cpuinfo("cpu\\s+: ");
  380.             $model      = &cpuinfo("cpu model\\s+: ");
  381.             $model      = "$model (" . &cpuinfo("system type") . ")";
  382.             $mhz        = &cpuinfo("cycle frequency \\[Hz\\]\\s+: ");
  383.             $mhz        = ($mhz / 1000000);
  384.             $mhz        = sprintf("%.2f", $mhz);
  385.             $cpu        = "$cpu $model ($mhz MHz)";
  386.             $smp        = &cpuinfo("cpus detected\\s+: ");
  387.         }
  388.         if($arm) {
  389.             $cpu        = &cpuinfo("Processor\\s+: ");
  390.         }
  391.         if ($armv6l || $armv7l) {
  392.             $cpu        = &cpuinfo("model name\\s+: ");
  393.             $cpu        =~ s/-compatible//;
  394.             $cpu        =~ s/processor //;
  395.             if (-r "/sys/bus/cpu/devices/cpu0/cpufreq/scaling_cur_freq") {
  396.                 $mhz        = `cat /sys/bus/cpu/devices/cpu0/cpufreq/scaling_cur_freq`;
  397.                 $mhz        = ($mhz / 1000);
  398.                 $cpu        = sprintf("%s (%.2f MHz)", $cpu, $mhz);
  399.             }
  400.             @smp        = grep(/processor\s+: /, @cpuinfo);
  401.             if (scalar @smp > 0) {
  402.                 $smp        = scalar @smp;
  403.             }
  404.         }
  405.         if($i686 || $i586 || $x86_64) {
  406.             $cpu        = &cpuinfo("model name\\s+: ");
  407.             $cpu        =~ s/(.+) CPU family\t+\d+MHz/$1/g;
  408.             $cpu        =~ s/(.+) CPU .+GHz/$1/g;
  409.             $mhz        = &cpuinfo("cpu MHz\\s+: ");
  410.             $mhz        =~ s/^\s*//g;
  411.             $cpu        = "$cpu ($mhz MHz)";
  412.             @smp        = grep(/processor\s+: /, @cpuinfo);
  413.             $smp        = scalar @smp;
  414.         }
  415.         if($ia64) {
  416.             $cpu        = &cpuinfo("vendor\\s+: ");
  417.             $model      = &cpuinfo("family\\s+: ");
  418.             $mhz        = &cpuinfo("cpu MHz\\s+: ");
  419.             $mhz        = sprintf("%.2f", $mhz);
  420.             $cpu        = "$cpu $model ($mhz MHz)";
  421.             @smp        = grep(/processor\s+: /, @cpuinfo);
  422.             $smp        = scalar @smp;
  423.         }
  424.         if($mips) {
  425.             $cpu        = &cpuinfo("cpu\\s+: ");
  426.             $model      = &cpuinfo("cpu model\\s+: ");
  427.             $cpu        = "$cpu $model";
  428.         }
  429.         if($parisc || $parisc64) {
  430.             $cpu        = &cpuinfo("cpu\\s+: ");
  431.             $model      = &cpuinfo("model name\\s+: ");
  432.             $mhz        = &cpuinfo("cpu MHz\\s+: ");
  433.             $mhz        = sprintf("%.2f", $mhz);
  434.             $cpu        = "$model $cpu ($mhz MHz)";
  435.             @smp        = grep(/processor\s+: /, @cpuinfo);
  436.             $smp        = scalar @smp;
  437.         }
  438.         if($ppc || $ppc64) {
  439.             $cpu        = &cpuinfo("cpu\\s+: ");
  440.             $mhz        = &cpuinfo("clock\\s+: ");
  441.             $mhz        =~ s/^(\d+\.\d{3})\d*\s*MHz/$1 MHz/;
  442.             $cpu        =~ s/, altivec supported//;
  443.             if($cpu =~ /^(PPC)*9.+/) {
  444.                 $model      = "IBM PowerPC G5";
  445.             } elsif($cpu =~ /^74.+/) {
  446.                 $model      = "Motorola PowerPC G4";
  447.             } else {
  448.                 $model      = "IBM PowerPC G3";
  449.             }
  450.             $cpu        = "$model $cpu ($mhz)";
  451.             $smp        = `/bin/grep -c -e '^processor\\s*:\\s*[0-9]\\+' /proc/cpuinfo`; chomp($smp);
  452.         }
  453.         if($s390 || $s390x) {
  454.             $cpu        = &cpuinfo("vendor_id\\s+: ");
  455.             $smp        = &cpuinfo("processors\\s+: ");
  456.         }
  457.         if($sh) {
  458.             $cpu            = &cpuinfo("cpu family\\s+: ");
  459.             $model          = &cpuinfo("cpu type\\s+: ");
  460.             $mhz            = &cpuinfo("cpu_clk\\s+: ");
  461.             $cpu            = "$cpu $model ($mhz MHz)";
  462.         }
  463.  
  464.         if($sparc || $sparc64) {
  465.             $cpu            = &cpuinfo("cpu\\s+: ");
  466.             $model          = &cpuinfo("type\\s+: ");
  467.             $cpu            = "$model $cpu";
  468.             $mhz            = &cpuinfo("Cpu0ClkTck\\s+: ");
  469.             $mhz            = (hex($mhz) / 1000000);
  470.             $cpu            = "$cpu ($mhz MHz)";
  471.             $smp            = &cpuinfo("ncpus active\\s+: ");
  472.         }
  473.       } elsif($sun) {
  474.             my $osp     = `uname -p`; chomp($osp);
  475.             if($osv =~ /^5\.11/ || ($osv =~ /^5\.10/ && $osp =~ "i386")) {
  476.                 $cpu    = `/usr/sbin/psrinfo -vp | tail -1`; chomp($cpu);
  477.                 $mhz    = `/usr/sbin/psrinfo -vp | grep MHz | awk '{ print "(" \$(NF-1) " " \$NF}' | tail -1`; chomp($mhz);
  478.                 $cpu    = "$cpu $mhz";
  479.                 $smp    = `/usr/sbin/psrinfo -p`; chomp($smp);
  480.                 $cpu    =~  s/^\s*//;
  481.             }
  482.             if($osp =~ "sparc") {
  483.                 $cpu    = `/usr/platform/${osm}/sbin/prtdiag | head -1`; chomp($cpu);
  484.                 $cpu    =~ s/.*${osm} //;
  485.                 $smp    = `/usr/sbin/psrinfo | wc -l`;
  486.             }
  487.     } elsif($darwin) {
  488.         $cpu        = `hostinfo | grep 'Processor type' | cut -f2 -d':'`; chomp($cpu);
  489.         $cpu        =~ s/^\s*(.+)\s*$/$1/g;
  490.         if($cpu =~ /^ppc7.+/) {
  491.             $cpu    = "Motorola PowerPC G4";
  492.         }
  493.         if($cpu =~ /^ppc970/) {
  494.             $cpu    = "Motorola PowerPC G5";
  495.         }
  496.         if ($cpu =~ /^i486/) {
  497.             $cpu    = `sysctl -n machdep.cpu.brand_string`; chomp($cpu);
  498.         }
  499.         $mhz        = `$sysctl -n hw.cpufrequency`; chomp($mhz);
  500.         $mhz        = sprintf("%.2f", $mhz / 1000000);
  501.         $cpu        = "$cpu ($mhz MHz)";
  502.         $smp        = `hostinfo | grep "physically available" | cut -f1 -d' '`; chomp($smp);
  503.     }
  504.     if($smp && $smp gt 1) {
  505.         $cpu = "$smp x $cpu";
  506.     }
  507.     $cpu    =~ s/\s*@\s*\d*\.\d*\s*GHz//;
  508.     $cpu    =~ s/^\s+//;
  509.     $cpu    =~ s/\(R\)//gi;
  510.     $cpu    =~ s/\(tm\)//gi;
  511.     $cpu    =~ s/\([^(]*GenuineIntel[^)]*\)//;
  512.     $cpu    =~ s/\s*processor//gi;
  513.     $cpu    =~ s/\s*CPU//gi;
  514.     $cpu    =~ s/ +/ /g;
  515. }
  516.  
  517. sub batteryacpi {
  518.     my $data = "";
  519.     my ($bfull, $bcur, @dirs, $dir);
  520.  
  521.     if (opendir(DIR, '/proc/acpi/battery/')) {
  522.         @dirs = grep { !/^\./ } readdir (DIR);
  523.         closedir(DIR);
  524.         foreach $dir (@dirs) {
  525.             $bfull = "";
  526.             $bcur = "";
  527.             if(open(FD, '/proc/acpi/battery/'.$dir.'/info')) {
  528.                 while (<FD>) {
  529.                     if (/^last full capacity:\ +([0-9]+)/) {
  530.                         $bfull = $1;
  531.                     }
  532.                 }
  533.                 close(FD);
  534.             }
  535.             if(open(FD, '/proc/acpi/battery/'.$dir.'/state')) {
  536.                 while (<FD>) {
  537.                     if (/^remaining capacity:\ +([0-9]+)/) {
  538.                         $bcur = $1;
  539.                     }
  540.                 }
  541.                 close(FD);
  542.             }
  543.             if ($bfull ne "") {
  544.                 if ($data ne "") {
  545.                     $data = $data." ";
  546.                 }
  547.                 $data = $data.sprintf("%.0f",($bcur/$bfull*100));
  548.                 $data = $data."%";
  549.             }
  550.         }
  551.     }
  552.     return $data;
  553. }
  554.  
  555. sub battery {
  556.     $data = "";
  557.  
  558.     if ( -f '/proc/apm') {
  559.         if(open(FD, '/proc/apm')) {
  560.             while($stream = <FD>) {
  561.                 $data .= $stream;
  562.                 @data = split(/\n/, $data);
  563.             }
  564.             close(FD);
  565.         }
  566.         $data = $data[0];
  567.         $data =~ s/.+\s(\d+%).+/$1/;
  568.     } elsif ( -d '/proc/acpi/battery') {
  569.         $data=&batteryacpi;
  570.     }
  571.     return $data;
  572. }
  573.  
  574. sub cpuinfo {
  575.     my $string = shift;
  576.     @arr = grep(/$string/, @cpuinfo);
  577.     $var = join("\n", $arr[0]);
  578.     @arr = split(/: /, $var);
  579.     $var = $arr[1];
  580.     return $var;
  581. }
  582.  
  583. sub diskusage {
  584.     if($irix || $irix64) {
  585.         $vara = `$df | grep dev | awk '{ sum+=\$3 / 1024 / 1024}; END { print sum }'`; chomp($vara);
  586.         $vard = `$df | grep dev | awk '{ sum+=\$4 / 1024 / 1024}; END { print sum }'`; chomp($vard);
  587.     } elsif($sun) {
  588.         $vara = `$df | grep -v swap | grep -v libc | awk '{ sum+=\$2 / 1024 / 1024}; END { print sum }'`; chomp($vara);
  589.         $vard = `$df | grep -v swap | grep -v libc | awk '{ sum+=\$3 / 1024 / 1024}; END { print sum }'`; chomp($vard);
  590.     } elsif($freebsd) {
  591.         if ($isJail == '1') { # in jails, storage is listed as /mnt/ mountpoints from the host, not as /dev/ devices
  592.             $vara = `$df | grep -v swap | awk '{ sum+=\$2 / 1024 / 1024}; END { print sum }'`; chomp($vara);
  593.             $vard = `$df | grep -v swap | awk '{ sum+=\$3 / 1024 / 1024}; END { print sum }'`; chomp($vard);
  594.         } else {
  595.             $vara = `$df | grep dev | awk '{ sum+=\$2 / 1024 / 1024}; END { print sum }'`; chomp($vara);
  596.             $vard = `$df | grep dev | awk '{ sum+=\$3 / 1024 / 1024}; END { print sum }'`; chomp($vard);
  597.         }
  598.     } else  {
  599.         $vara = `$df | grep dev | awk '{ sum+=\$2 / 1024 / 1024}; END { print sum }'`; chomp($vara);
  600.         $vard = `$df | grep dev | awk '{ sum+=\$3 / 1024 / 1024}; END { print sum }'`; chomp($vard);
  601.     }
  602.     if ($vara eq "" or $vara == 0 ) {
  603.         return "0GB/0GB (0%)";
  604.     } else {
  605.         $varp = sprintf("%.2f", $vard / $vara * 100);
  606.         $vara = sprintf("%.2f", $vara);
  607.         $vard = sprintf("%.2f", $vard);
  608.         return $vard."GB/".$vara."GB ($varp%)";
  609.     }
  610. }
  611.  
  612. sub loadaverage {
  613.     $var = `uptime`; chomp($var);
  614.     @arr = split(/averages*: /, $var, 2);
  615.     if($darwin) {
  616.         @arr = split(/ +/, $arr[1], 2);
  617.     } else {
  618.         @arr = split(/, /, $arr[1], 2);
  619.     }
  620.     $var = $arr[0];
  621.     return $var;
  622. }
  623.  
  624. sub meminfo {
  625.     my $string = shift;
  626.     @arr = grep(/^$string/, @meminfo);
  627.     $var = join("\n", $arr[0]);
  628.     @arr = split(/\s+/, $var);
  629.     $var = $arr[1];
  630.     return $var;
  631. }
  632.  
  633. sub memoryusage {
  634.     if($linux) {
  635.         if(($l26)||($l3)) {
  636.             $vara = &meminfo("MemTotal:") * 1024;
  637.             $varb = &meminfo("Buffers:") * 1024;
  638.             $varc = &meminfo("Cached:") * 1024;
  639.             $vard = &meminfo("MemFree:") * 1024;
  640.         } else {
  641.             @arr = grep(/Mem:/, @meminfo);
  642.             $var = join("\n", @arr);
  643.             @arr = split(/\s+/, $var);
  644.             $vara = $arr[1];
  645.             $varb = $arr[5];
  646.             $varc = $arr[6];
  647.             $vard = $arr[3];
  648.         }
  649.         $vard = ($vara - $vard) - $varb - $varc;
  650.     } elsif($darwin) {
  651.         $vard = `vm_stat | grep 'Pages active' | awk '{print \$3}'` * 4096;
  652.         $vara = `$sysctl -n hw.memsize`;
  653.     } elsif($sun) {
  654.         $vara = `/usr/sbin/prtconf | grep "Mem" | awk '{print \$3}'` * 1024 * 1024;
  655.         $vard = `/bin/vmstat 1 2 | tail -1 | awk '{print \$5 * 1024}'`;
  656.         $vard = $vara - $vard
  657.     } elsif($irix || $irix64) {
  658.         $var = `top -d1 | grep Memory`; chomp($var);
  659.         $vara = $var;
  660.         $vard = $var;
  661.         $vara =~ s/^.* ([0-9]*)M max.*$/$1/;
  662.         $vara *= 1024 * 1024;
  663.         $vard =~ s/^.* ([0-9]*)M free,.*$/$1/;
  664.         $vard = $vara - ($vard * 1024 * 1024);
  665.     } elsif($netbsd) {
  666.         $vard = `vmstat -t | tail -n 1 | awk '{print \$8}'`;
  667.         $vard = $vard * 4096;
  668.         $vara = `sysctl -n hw.physmem`;
  669.     } else {
  670.         $vard = `vmstat -s | grep 'pages active' | awk '{print \$1}'` * `vmstat -s | grep 'per page' | awk '{print \$1}'`;
  671.         $vara = `$sysctl -n hw.physmem`;
  672.     }
  673.     $varp = sprintf("%.2f", $vard / $vara * 100);
  674.     $vara = sprintf("%.2f", $vara / 1024 / 1024);
  675.     $vard = sprintf("%.2f", $vard / 1024 / 1024);
  676.     return $vard."MB/".$vara."MB ($varp%)";
  677. }
  678.  
  679. sub networkinfobsd {
  680.     $varc = shift;
  681.     $vard = shift;
  682.     @arr = grep(/$varc/, @netstat);
  683.     @arr = grep(/Link/, @arr);
  684.     $var = join("\n", @arr);
  685.     @arr = split(/\s+/, $var);
  686.     $var = $arr[$vard] / 1024 / 1024 / 1024;
  687.     $var = sprintf("%.2f", $var);
  688.     return $var;
  689. }
  690.  
  691. sub networkinfolinux {
  692.     $varc = shift;
  693.     $vard = shift;
  694.     @arr = grep(/$varc/, @netdev);
  695.     $var = join("\n", @arr);
  696.     @arr = split(/:\s*/, $var);
  697.     @arr = split(/\s+/, $arr[1]);
  698.     $var = $arr[$vard] / 1024 / 1024 / 1024;
  699.     $var = sprintf("%.2f", $var);
  700.     return $var;
  701. }
  702.  
  703. sub networktraffic {
  704.     $vara = 0;
  705.     $varz = "";
  706.     $varb = scalar @nic;
  707.     if($nic[$vara] ne "") {
  708.         while($vara lt $varb) {
  709.             if($nic[$vara] ne "") {
  710.                 if ($varz eq "") {
  711.                     $varz = $col2." - ".$col1;
  712.                 }
  713.                 if($darwin || $freebsd || $dragonfly) {
  714.                     $varx = &networkinfobsd($nic[$vara], 6);
  715.                     $vary = &networkinfobsd($nic[$vara], 9);
  716.                 }
  717.                 if($netbsd || $openbsd) {
  718.                     $varx = &networkinfobsd($nic[$vara], 4);
  719.                     $vary = &networkinfobsd($nic[$vara], 5);
  720.                 }
  721.                 if($sun) {
  722.                     $varx = &networkinfobsd($nic[$vara], 4);
  723.                     $vary = &networkinfobsd($nic[$vara], 6);
  724.                 }
  725.                 if($linux) {
  726.                     $varx = &networkinfolinux($nic[$vara], 0);
  727.                     $vary = &networkinfolinux($nic[$vara], 8);
  728.                 }
  729.                 $varz .= $nicname[$vara]." Traffic (".$nic[$vara].")".$col2.": ".$col1.$varx."GB In/".$vary."GB Out - ";
  730.             }
  731.             $vara++;
  732.         }
  733.         return $varz;
  734.     }
  735. }
  736.  
  737. sub openfile {
  738.     my $string = shift;
  739.     $data = "";
  740.     if(open(FD, $string)) {
  741.         while($stream = <FD>) {
  742.             $data .= $stream;
  743.             @data = split(/\n/, $data);
  744.         }
  745.         close(FD);
  746.     }
  747.     return @data;
  748. }
  749.  
  750. sub processes {
  751.     if($irix || $irix64 || $sun) {
  752.         $var = `ps -e | grep -v PID | wc -l`;
  753.     } else {
  754.         $var = `ps ax | grep -v PID | wc -l`;
  755.     }
  756.     chomp($var);
  757.     $var = $var;
  758.     $var =~ s/^\s+//;
  759.     $var =~ s/\s+$//;
  760.     return $var;
  761. }
  762.  
  763. sub uptime {
  764.     if($irix || $irix64 || $d9 || $sun) {
  765.         $var = `uptime`; chomp($var);
  766.         if($var =~ /day/) {
  767.             $var =~ s/^.* ([0-9]*) day.* ([0-9]*):([0-9]*), .*$/$1d $2h $3m/;
  768.         } elsif($var =~ /min/) {
  769.             $var =~ s/^.* ([0-9]*) min.*$/0d 0h $1m/;
  770.         } else {
  771.             $var =~ s/^.* ([0-9]*):([0-9]*),.*$/0d $1h $2m/;
  772.         }
  773.         return $var;
  774.     } else {
  775.         if($freebsd || $dragonfly) {
  776.             $var = `$sysctl -n kern.boottime | awk '{print \$4}'`;
  777.         }
  778.         if($netbsd || $openbsd || $darwin) {
  779.             $var = `$sysctl -n kern.boottime`;
  780.         }
  781.         if($linux) {
  782.             @arr = split(/ /, $uptime[0]);
  783.             $varx = $arr[0];
  784.         } else {
  785.             chomp($var);
  786.             $var =~ s/,//g;
  787.             $vary = `date +%s`; chomp($vary);
  788.             $varx = $vary - $var;
  789.         }
  790.         $varx = sprintf("%2d", $varx);
  791.         $vard = floor($varx / 86400);
  792.         $varx %= 86400;
  793.         $varh = floor($varx / 3600);
  794.         $varx %= 3600;
  795.         $varm = floor($varx / 60);
  796.         if($vard eq 0) { $vard = ''; } elsif($vard >= 1) { $vard = $vard.'d '; }
  797.         if($varh eq 0) { $varh = ''; } elsif($varh >= 1) { $varh = $varh.'h '; }
  798.         if($varm eq 0) { $varm = ''; } elsif($varm >= 1) { $varm = $varm.'m'; }
  799.         return $vard.$varh.$varm;
  800.     }
  801. }
  802.  
  803. sub users {
  804.     $var = `uptime`; chomp($var);
  805.     $var =~ s/^.* +(.*) user.*$/$1/;
  806.     return $var;
  807. }
  808.  
  809. my $output;
  810. if($showHostname)       { $output   = $col1."Hostname".$col2.": ".$col1.$osn.$col2." - "; }
  811. if($showOS)             { $output .= $col1."OS".$col2.": ".$col1.$uname.$col2." - "; }
  812. if($linux && $showDistro && length $distro > 0 ) {
  813.     $output .= $col1."Distro".$col2.": ".$col1.$distro;
  814.     if (length $distrov > 0) {
  815.         $output .= " ".$distrov;
  816.     }
  817.     $output .= $col2." - ";
  818. }
  819. if($showCPU)            { $output .= $col1."CPU".$col2.": ".$col1.$cpu.$col2." - "; }
  820. if($showProcesses)      { $output .= $col1."Processes".$col2.": ".$col1.&processes.$col2." - "; }
  821. if($showUptime)         { $output .= $col1."Uptime".$col2.": ".$col1.&uptime.$col2." - "; }
  822. if($showUsers)          { $output .= $col1."Users".$col2.": ".$col1.&users.$col2." - "; }
  823. if($showLoadAverage)    { $output .= $col1."Load Average".$col2.": ".$col1.&loadaverage.$col2." - "; }
  824. if($showBattery)        { $output .= $col1."Battery".$col2.": ".$col1.&battery.$col2." - "; }
  825. if($showMemoryUsage)    { $output .= $col1."Memory Usage".$col2.": ".$col1.&memoryusage.$col2." - "; }
  826. if($showDiskUsage)      { $output .= $col1."Disk Usage".$col2.": ".$col1.&diskusage; }
  827. if($showNetworkTraffic) { $output .= &networktraffic; }
  828. $output =~ s/ - $//g;
  829. print "$output\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement