Last active
March 18, 2016 09:19
-
-
Save n7st/cf53d3c6c712e1d20e2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl -w | |
use strict; | |
use warnings; | |
use Math::Round; | |
sysinfo(); | |
sub sysinfo { | |
my ($args, $server, $win_item); | |
my $basic = basic(); | |
my $uptime = uptime(); | |
my $memory = memory(); | |
my $cpu = cpu(); | |
my $output = join(' | ', ($basic, $memory, $cpu, $uptime)); | |
print "$output\n"; | |
} | |
sub basic { | |
my ($hostname, $uname, $procs); | |
chomp ($hostname = `hostname`); | |
chomp ($uname = `uname -srm`); | |
($hostname) = $hostname =~ /([^\.]*)/; | |
return sprintf('%s/%s', $hostname, $uname); | |
} | |
sub uptime { | |
my @uptime = split(/ /, `uptime`); | |
my ($days, $hours, $minutes); | |
if (grep { $_ eq 'mins,' } @uptime) { | |
($hours, $minutes) = (0, $uptime[6]); | |
$days = $uptime[3]; | |
} elsif (grep { $_ eq 'hrs,' } @uptime) { | |
($hours, $minutes) = ($uptime[6], 0); | |
$days = $uptime[3]; | |
} else { | |
($hours, $minutes) = split(/:/, $uptime[5]); | |
$days = $uptime[3]; | |
$minutes =~ s/,//; | |
} | |
return sprintf('up: %dd %dh %dm', $days, $hours, $minutes); | |
} | |
sub memory { | |
my ($size, $free); | |
my @memory = split(/\n/, `sysctl -a | grep "hw.*mem"`); | |
my %info; | |
foreach my $line (@memory) { | |
my ($handle, $value) = split(/:\ /, $line); | |
$info{$handle} = $value; | |
} | |
my $hw_total_gb = nearest(.01, ($info{'hw.physmem'} /= 1024) / 1024); | |
my $used_total_gb = nearest(.01, ($info{'hw.usermem'} /= 1024) / 1024); | |
my $percentage = calc_percentage($hw_total_gb, $used_total_gb); | |
return sprintf('mem: %d/%dMB (%d%%)', $used_total_gb, $hw_total_gb, $percentage); | |
} | |
sub cpu { | |
my @cpu = split(/\n/, `sysctl -a | grep -E "hw.(model|ncpu)"`); | |
my ($core_handle, $cores) = split(/: /, $cpu[1]); | |
my ($proc_handle, $proc) = split(/: /, $cpu[0]); | |
return sprintf('cpu: %s (%d core)', $proc, $cores); | |
} | |
sub calc_percentage { | |
my ($haystack, $needle) = @_; | |
return nearest(.001, ($needle * 100) / $haystack) || 0; | |
} | |
__END__ | |
=head1 NAME | |
sysinfo-bsd | |
=head1 DESCRIPTION | |
Get a BSD system's information and stats - CPU, RAM, uptime, disk etc. | |
Based on sysinfo-dg, may still contain some boilerplate from that script. | |
=head1 SYNOPSIS | |
perl sysinfo-bsd.pl | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment