enhancement: add services to sysinfo.json api

Change-Id: I09769ef110a6210513efc4bc23908be9ac7ddf6a
This commit is contained in:
Darryl Quinn 2016-08-19 16:14:25 -05:00 committed by K5DLQ
parent 8bf9d0948d
commit 5eb457b11a
1 changed files with 51 additions and 16 deletions

View File

@ -32,6 +32,9 @@
and be marked in reasonable ways as differentiate it from the original and be marked in reasonable ways as differentiate it from the original
version. version.
API VERSIONS:
1.0 - initial release
1.1 - added services
=cut =cut
BEGIN {push @INC, '/www/cgi-bin'}; BEGIN {push @INC, '/www/cgi-bin'};
@ -42,7 +45,7 @@ read_query_string();
my %info; my %info;
$info{"api_version"}="1.0"; $info{"api_version"}="1.1";
# ============ GENERATE INFO HASH ========= # ============ GENERATE INFO HASH =========
$node=nvram_get("node"); $node=nvram_get("node");
@ -147,21 +150,53 @@ if($parms{"hosts"} )
{ {
print ",\"hosts\":["; print ",\"hosts\":[";
$mystr=""; $mystr="";
foreach(`cat /var/run/hosts_olsr`) $hostsfile="/var/run/hosts_olsr";
if ( -e $hostsfile)
{ {
$line = $_; if (open(FILE, "<$hostsfile")) {
next unless /^(\d+\.\d+\.\d+\.\d+)\s*(\S+)/; while($line = <FILE>)
{
next unless $line =~ /^(\d+\.\d+\.\d+\.\d+)\s*(\S+)/;
if ($line !~ /dtdlink|mid[0-9]+\./ ) { if ($line !~ /dtdlink|mid[0-9]+\./ ) {
$mystr .= sprintf "{\"name\":\"%s\",\"ip\":\"%s\"},",$2,$1; $mystr .= sprintf "{\"name\":\"%s\",\"ip\":\"%s\"},",$2,$1;
} }
} }
close FILE;
} else {
$mystr=sprintf "{\"error\":\"Could not open: %s\"",$hostsfile;
}
}
chop($mystr); chop($mystr);
print $mystr; print $mystr;
print "]"; print "]";
############## HOSTS ############## HOSTS
} }
if($parms{"services"} )
{
print ",\"services\":[";
$mystr="";
$servicesfile="/var/run/services_olsr";
if ( -e $servicesfile )
{
if (open(FILE, "<$servicesfile")) {
while($line = <FILE>)
{
next unless $line =~ /^(.*)\|(.+)\|(.*)\t+\s+.*/;
$mystr .= sprintf "{\"link\":\"%s\",\"protocol\":\"%s\",\"name\":\"%s\"},",$1,$2,$3;
}
close FILE ;
} else {
$mystr=sprintf "{\"error\":\"Could not open: %s\"",$servicesfile;
}
}
chop($mystr);
print $mystr;
print "]";
############## SERVICES
}
print "}"; # ROOT print "}"; # ROOT
############################## ##############################