Files
7ca0582b3c Refactor Perl scripts with Code Quality findings (#6594)
* add perl syntax checking for scripts

* Update .github/workflows/syntax.yml

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* only show syntax errors

* Add support for ss

Fixed shell injection risk from piped netstat | grep by removing shell pipelines.
Added use strict; use warnings; and localized variables.
Added runtime fallback to ss when netstat is unavailable, with a clear failure when neither exists.

* Fix shell injection risk

apply fixes (safe open, strict/warnings, key whitelist, error handling).

* Update query_unix_partitions.pl

* Update unix_users.pl

add use strict/warnings  to flag issues
add better error handling

* Update weatherbug.pl

* add better suport for gree bsd

* Update loadavg.pl

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: TheWitness <thewitness@cacti.net>
2026-01-31 20:25:42 -05:00

84 lines
1.9 KiB
Prolog

#!/usr/bin/env perl
use strict;
use warnings;
use IPC::Open3;
use Symbol 'gensym';
delete @ENV{qw(PATH)};
$ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin';
# take care for tcp:hostname or TCP:ip@
my $host = $ARGV[0] // '';
$host =~ s/^(?:tcp|udp)6?://gis;
# leave IPv6 in tact
my $host_copy = $host;
if (($host_copy =~ tr/://) == 0) {
$host =~ s/:[0-9]{1,5}$//gis;
}
# Addition2
($host) = $host =~ /^([\w.:-]+)$/;
# always have the language in english
$ENV{LANG}='en_US.UTF-8';
#
# Get the OS name
#
my $osname = "$^O";
sub run_ping {
my (@cmd) = @_;
my $err = gensym;
my $pid = open3(undef, my $out, $err, @cmd);
my @lines = (<$out>, <$err>);
waitpid($pid, 0);
return join('', @lines);
}
my @ping_cmd;
if ($osname =~ /solaris/i) {
# Solaris needs a different ping command
@ping_cmd = ('ping', '-s', $host, '64', '1');
} else {
@ping_cmd = ('ping', '-c', '1', $host);
}
my $output = run_ping(@ping_cmd);
my $ping = '';
for my $line (split /\n/, $output) {
if ($line =~ /(icmp_[s|r]eq.*t(ime|emps)|unknown host|Unknown host|not supported|Name or service not known|No address associated with name)/) {
$ping = $line;
last;
}
}
chomp($ping);
if (($ping =~ 'unknown host') || ($ping =~ 'Unknown host') || ($ping =~ 'not supported') || ($ping =~ 'Name or service not known') || ($ping =~ 'No address associated with name')) {
if ((-f '/bin/ping6') || (-f '/sbin/ping6')) {
my $output6 = run_ping('ping6', '-c', '1', $host);
for my $line (split /\n/, $output6) {
if ($line =~ /(icmp_[s|r]eq.*t(ime|emps))/) {
$ping = $line;
last;
}
}
chomp($ping);
}
}
if ($ping =~ m/(.*t(?:ime|emps)=)([\w\.]*).*(ms|usec)/) {
my ($value, $unit) = ($2, $3);
if ($value eq '') {
print 'U'; # avoid cacti errors, but do not fake rrdtool stats
} elsif ($unit eq 'usec') {
print $value/1000; # re-calculate in units of 'ms'
} else {
print $value;
}
} else {
print 'U';
}