You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
210 lines
8.6 KiB
210 lines
8.6 KiB
7 years ago
|
#!/usr/bin/perl
|
||
|
#
|
||
|
# This is the master daemon that manages all periodically run processes on Striker dashboards and Anvil!
|
||
|
# nodes.
|
||
|
#
|
||
|
use strict;
|
||
|
use warnings;
|
||
7 years ago
|
use Anvil::Tools;
|
||
7 years ago
|
|
||
|
my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0];
|
||
|
my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
|
||
|
if (($running_directory =~ /^\./) && ($ENV{PWD}))
|
||
|
{
|
||
|
$running_directory =~ s/^\./$ENV{PWD}/;
|
||
|
}
|
||
|
|
||
7 years ago
|
my $$anvil = Anvil::Tools->new();
|
||
|
$$anvil->Log->level({set => 2});
|
||
7 years ago
|
|
||
7 years ago
|
$$anvil->Storage->read_config({file => "/etc/anvil/anvil.conf"});
|
||
|
my $connections = $$anvil->Database->connect({
|
||
|
sql_file => $$anvil->data->{sys}{database}{schema},
|
||
7 years ago
|
test_table => "network_interfaces",
|
||
|
});
|
||
7 years ago
|
|
||
7 years ago
|
# Turn off buffering so that the pinwheel will display while waiting for the SSH call(s) to complete.
|
||
|
$| = 1;
|
||
|
|
||
7 years ago
|
report_network($anvil);
|
||
7 years ago
|
|
||
|
exit(0);
|
||
|
|
||
|
#############################################################################################################
|
||
|
# Functions #
|
||
|
#############################################################################################################
|
||
|
|
||
|
# This reports the current network interface states, tracked by the MAC address.
|
||
|
sub report_network
|
||
|
{
|
||
7 years ago
|
my ($anvil) = @_;
|
||
7 years ago
|
|
||
7 years ago
|
# Write out the data in json format.
|
||
7 years ago
|
my $directory = $$anvil->data->{path}{sysfs}{network_interfaces};
|
||
7 years ago
|
local(*DIRECTORY);
|
||
7 years ago
|
$$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0018", variables => { directory => $directory }});
|
||
7 years ago
|
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";
|
||
7 years ago
|
$$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { full_path => $full_path }});
|
||
7 years ago
|
if (-d $full_path)
|
||
|
{
|
||
|
# Pull out the data I want.
|
||
|
my $interface = $file;
|
||
7 years ago
|
my $mac_address = $$anvil->Storage->read_file({file => $full_path."/address"});
|
||
|
my $link_state = $$anvil->Storage->read_file({file => $full_path."/carrier"});
|
||
|
my $mtu = $$anvil->Storage->read_file({file => $full_path."/mtu"});
|
||
|
my $duplex = $$anvil->Storage->read_file({file => $full_path."/duplex"}); # full or half?
|
||
|
my $operational = $$anvil->Storage->read_file({file => $full_path."/operstate"}); # up or down
|
||
|
my $speed = $link_state ? $$anvil->Storage->read_file({file => $full_path."/speed"}) : 0; # Mbps (ie: 1000 = Gbps), gives a very high number for unplugged link
|
||
7 years ago
|
if ($speed > 100000)
|
||
|
{
|
||
7 years ago
|
# NOTE: This is probably 0 now... Though someday >100 Gbps will be reasonable
|
||
|
# and we'll need to change this.
|
||
7 years ago
|
$speed = 0;
|
||
|
}
|
||
7 years ago
|
|
||
|
# Find the media, if possible.
|
||
|
my $media = "unknown";
|
||
7 years ago
|
my $ethtool = $$anvil->System->call({shell_call => $$anvil->data->{path}{exe}{ethtool}});
|
||
7 years ago
|
foreach my $line (split/\n/, $ethtool)
|
||
|
{
|
||
|
if ($line =~ /Supported ports: \[ (.*?) \]/i)
|
||
|
{
|
||
|
$media = lc($1);
|
||
|
last;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# Log
|
||
7 years ago
|
$$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
|
||
7 years ago
|
interface => $interface,
|
||
|
mac_address => $mac_address,
|
||
|
link_state => $link_state,
|
||
|
mtu => $mtu,
|
||
|
duplex => $duplex,
|
||
|
operational => $operational,
|
||
|
speed => $speed,
|
||
7 years ago
|
media => $media,
|
||
7 years ago
|
}});
|
||
7 years ago
|
|
||
7 years ago
|
$$anvil->Database->insert_or_update_network_interfaces({
|
||
7 years ago
|
network_interface_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,
|
||
7 years ago
|
});
|
||
|
|
||
7 years ago
|
}
|
||
|
}
|
||
|
closedir(DIRECTORY);
|
||
7 years ago
|
|
||
7 years ago
|
### TODO: Create $anvil "ip" table and record IPs on this system, linking back to $anvil interface, bond or
|
||
7 years ago
|
### bridge.
|
||
|
# Run 'ip addr' to see what IPs are in use.
|
||
7 years ago
|
$$anvil->System->get_ips;
|
||
7 years ago
|
|
||
7 years ago
|
# Write out the XML file and JSON file.
|
||
|
my $order = 1;
|
||
|
my $network_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||
|
$network_xml .= "<network>\n";
|
||
|
my $network_json = "{\"networks\":[\n";
|
||
|
my $query = "
|
||
|
SELECT
|
||
|
network_interface_mac_address,
|
||
|
network_interface_name,
|
||
|
network_interface_speed,
|
||
|
network_interface_mtu,
|
||
|
network_interface_link_state,
|
||
|
network_interface_operational,
|
||
|
network_interface_duplex,
|
||
|
network_interface_medium,
|
||
|
network_interface_bond_uuid,
|
||
|
network_interface_bridge_uuid
|
||
|
FROM
|
||
|
network_interfaces
|
||
|
WHERE
|
||
7 years ago
|
network_interface_host_uuid = ".$$anvil->data->{sys}{use_db_fh}->quote($$anvil->Get->host_uuid)."
|
||
7 years ago
|
ORDER BY
|
||
|
modified_date DESC
|
||
|
;";
|
||
7 years ago
|
$$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0124", variables => { query => $query }});
|
||
|
my $results = $$anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
|
||
7 years ago
|
my $count = @{$results};
|
||
7 years ago
|
$$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
|
||
7 years ago
|
results => $results,
|
||
|
count => $count,
|
||
|
}});
|
||
|
foreach my $row (@{$results})
|
||
|
{
|
||
|
my $network_interface_mac_address = $row->[0];
|
||
|
my $network_interface_name = $row->[1];
|
||
|
my $network_interface_speed = $row->[2];
|
||
|
my $network_interface_mtu = defined $row->[3] ? $row->[3] : "";
|
||
|
my $network_interface_link_state = $row->[4];
|
||
|
my $network_interface_operational = $row->[5];
|
||
|
my $network_interface_duplex = $row->[6];
|
||
|
my $network_interface_medium = defined $row->[7] ? $row->[7] : "";
|
||
|
my $network_interface_bond_uuid = defined $row->[8] ? $row->[8] : "";
|
||
|
my $network_interface_bridge_uuid = defined $row->[9] ? $row->[9] : "";
|
||
7 years ago
|
$$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
|
||
7 years ago
|
network_interface_mac_address => $network_interface_mac_address,
|
||
|
network_interface_name => $network_interface_name,
|
||
|
network_interface_speed => $network_interface_speed,
|
||
|
network_interface_mtu => $network_interface_mtu,
|
||
|
network_interface_link_state => $network_interface_link_state,
|
||
|
network_interface_operational => $network_interface_operational,
|
||
|
network_interface_duplex => $network_interface_duplex,
|
||
|
network_interface_medium => $network_interface_medium,
|
||
|
network_interface_bond_uuid => $network_interface_bond_uuid,
|
||
|
network_interface_bridge_uuid => $network_interface_bridge_uuid,
|
||
|
order => $order,
|
||
|
}});
|
||
|
$network_json .= " { \"name\":\"$network_interface_name\", \"mac\":\"$network_interface_mac_address\", \"link\":\"$network_interface_link_state\", \"speed\":\"$network_interface_speed\", \"mtu\":\"$network_interface_mtu\", \"duplex\":\"$network_interface_duplex\", \"state\":\"$network_interface_operational\", \"media\":\"$network_interface_medium\", \"bond\":\"$network_interface_bond_uuid\", \"bridge\":\"$network_interface_bridge_uuid\", \"order\":\"$order\" },\n";
|
||
|
$network_xml .= " <interface name=\"$network_interface_name\" mac=\"$network_interface_mac_address\" link=\"$network_interface_link_state\" speed=\"$network_interface_speed\" mtu=\"$network_interface_mtu\" duplex=\"$network_interface_duplex\" state=\"$network_interface_operational\" media=\"$network_interface_medium\" bond=\"$network_interface_bond_uuid\" bridge=\"$network_interface_bridge_uuid\" order=\"$order\" />\n";
|
||
|
$order++;
|
||
|
}
|
||
|
|
||
7 years ago
|
$network_json =~ s/,$//s;
|
||
|
$network_json .= "]}\n";
|
||
7 years ago
|
$$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { network_json => $network_json }});
|
||
7 years ago
|
|
||
7 years ago
|
$network_xml .= "</network>\n";
|
||
7 years ago
|
$$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { network_xml => $network_xml }});
|
||
7 years ago
|
|
||
7 years ago
|
### TODO: Set the 'status/network.json' name into 'striker.conf'
|
||
7 years ago
|
# Write the JSON file.
|
||
7 years ago
|
my $output_json = $$anvil->data->{path}{directories}{html}."/status/network.json";
|
||
|
$$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output_xml => $output_json }});
|
||
|
$$anvil->Storage->write_file({
|
||
7 years ago
|
file => $output_json,
|
||
|
body => $network_json,
|
||
|
overwrite => 1,
|
||
|
mode => "0644",
|
||
|
user => "apache",
|
||
|
group => "apache"
|
||
|
});
|
||
7 years ago
|
|
||
7 years ago
|
# Write the XML file.
|
||
7 years ago
|
my $output_xml = $$anvil->data->{path}{directories}{html}."/status/network.xml";
|
||
|
$$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output_xml => $output_xml }});
|
||
|
$$anvil->Storage->write_file({
|
||
7 years ago
|
file => $output_xml,
|
||
|
body => $network_xml,
|
||
|
overwrite => 1,
|
||
|
mode => "0644",
|
||
|
user => "apache",
|
||
|
group => "apache"
|
||
|
});
|
||
|
|
||
7 years ago
|
return(0);
|
||
7 years ago
|
}
|