#!/usr/bin/perl
#
# This is the master daemon that manages all periodically run processes on Striker dashboards and Anvil!
# nodes.
#
use strict;
use warnings;
use AN::Tools;
my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0];
my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
if (($running_directory =~ /^\./) && ($ENV{PWD}))
{
$running_directory =~ s/^\./$ENV{PWD}/;
}
my $an = AN::Tools->new();
$an->Log->level({set => 2});
$an->Storage->read_config({file => "/etc/striker/striker.conf"});
my $connections = $an->Database->connect({
sql_file => $an->data->{sys}{database}{schema},
test_table => "network_interfaces",
});
# Turn off buffering so that the pinwheel will display while waiting for the SSH call(s) to complete.
$| = 1;
report_network($an);
exit(0);
#############################################################################################################
# Functions #
#############################################################################################################
# This reports the current network interface states, tracked by the MAC address.
sub report_network
{
my ($an) = @_;
# Write out the data in json format.
my $network_json = "{\"networks\":[\n";
my $directory = $an->data->{path}{sysfs}{network_interfaces};
local(*DIRECTORY);
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0018", variables => { directory => $directory }});
opendir(DIRECTORY, $directory);
while(my $file = readdir(DIRECTORY))
{
next if $file eq ".";
next if $file eq "..";
next if $file eq "lo";
my $full_path = "$directory/$file";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { full_path => $full_path }});
if (-d $full_path)
{
# Pull out the data I want.
my $interface = $file;
my $mac_address = $an->Storage->read_file({file => $full_path."/address"});
my $link_state = $an->Storage->read_file({file => $full_path."/carrier"});
my $mtu = $an->Storage->read_file({file => $full_path."/mtu"});
my $duplex = $an->Storage->read_file({file => $full_path."/duplex"}); # full or half?
my $operational = $an->Storage->read_file({file => $full_path."/operstate"}); # up or down
my $speed = $link_state ? $an->Storage->read_file({file => $full_path."/speed"}) : 0; # Mbps (ie: 1000 = Gbps), gives a very high number for unplugged link
if ($speed > 100000)
{
# NOTE: This is probably 0 now... Though someday >100 Gbps will be reasonable
# and we'll need to change this.
$speed = 0;
}
# Find the media, if possible.
my $media = "unknown";
my $ethtool = $an->System->call({shell_call => $an->data->{path}{exe}{ethtool}});
foreach my $line (split/\n/, $ethtool)
{
if ($line =~ /Supported ports: \[ (.*?) \]/i)
{
$media = lc($1);
last;
}
}
# Log
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
interface => $interface,
mac_address => $mac_address,
link_state => $link_state,
mtu => $mtu,
duplex => $duplex,
operational => $operational,
speed => $speed,
media => $media,
}});
$an->Database->insert_or_update_network_interfaces({
network_interface_current_name => $interface,
network_interface_duplex => $duplex,
network_interface_link_state => $link_state,
network_interface_operational => $operational,
network_interface_mac_address => $mac_address,
network_interface_medium => $media,
network_interface_mtu => $mtu,
network_interface_speed => $speed,
});
$network_json .= " { \"name\":\"$interface\", \"mac\":\"$mac_address\", \"link\":\"$link_state\", \"mtu\":\"$mtu\" \"duplex\":\"$duplex\", \"state\":\"$operational\", \"speed\":\"$speed\", \"media\":\"$media\" },\n";
}
}
closedir(DIRECTORY);
$network_json =~ s/,$//s;
$network_json .= "]}\n";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { network_json => $network_json }});
### TODO: Set the 'status/network.json' name into 'striker.conf'
# Write the JSON file.
my $output_json = $an->data->{path}{directories}{html}."/status/network.json";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output_xml => $output_json }});
$an->Storage->write_file({
file => $output_json,
body => $network_json,
overwrite => 1,
mode => "0644",
user => "apache",
group => "apache"
});
return(0);
}