mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-27 19:23:24 +02:00
web: Create a function to show the service status
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
This commit is contained in:
@@ -97,6 +97,82 @@ sub system($) {
|
||||
return $rc;
|
||||
}
|
||||
|
||||
sub read_pids($) {
|
||||
my $pidfile = shift;
|
||||
|
||||
# Open the PID file
|
||||
open(PIDFILE, "<${pidfile}");
|
||||
|
||||
# Store all PIDs here
|
||||
my @pids = ();
|
||||
|
||||
# Read all PIDs
|
||||
while (<PIDFILE>) {
|
||||
chomp $_;
|
||||
|
||||
if (-d "/proc/$_") {
|
||||
push(@pids, $_);
|
||||
}
|
||||
}
|
||||
|
||||
# Close the PID file
|
||||
close(PIDFILE);
|
||||
|
||||
return @pids;
|
||||
}
|
||||
|
||||
sub find_pids($) {
|
||||
my $process = shift;
|
||||
|
||||
# Store all PIDs here
|
||||
my @pids = ();
|
||||
|
||||
foreach my $status (</proc/*/status>) {
|
||||
# Open the status file
|
||||
open(STATUS, "<${status}");
|
||||
|
||||
# Read the status file
|
||||
while (<STATUS>) {
|
||||
# If the name does not match, we break the loop immediately
|
||||
if ($_ =~ m/^Name:\s+(.*)$/) {
|
||||
last if ($process ne $1);
|
||||
|
||||
# Push the PID onto the list
|
||||
} elsif ($_ =~ m/^Pid:\s+(\d+)$/) {
|
||||
push(@pids, $1);
|
||||
|
||||
# Once we got here, we are done
|
||||
last;
|
||||
}
|
||||
}
|
||||
|
||||
# Close the status file
|
||||
close(STATUS);
|
||||
}
|
||||
|
||||
return @pids;
|
||||
}
|
||||
|
||||
sub get_memory_consumption() {
|
||||
my $memory = 0;
|
||||
|
||||
foreach my $pid (@_) {
|
||||
# Open the status file or skip on error
|
||||
open(STATUS, "/proc/${pid}/status") or next;
|
||||
|
||||
while (<STATUS>) {
|
||||
if ($_ =~ m/^VmRSS:\s+(\d+) kB/) {
|
||||
$memory += $1 * 1024;
|
||||
last;
|
||||
}
|
||||
}
|
||||
|
||||
close(STATUS);
|
||||
}
|
||||
|
||||
return $memory;
|
||||
}
|
||||
|
||||
# Function to remove duplicates from an array
|
||||
sub uniq { my %seen; grep !$seen{$_}++, @_ }
|
||||
|
||||
|
||||
@@ -896,4 +896,92 @@ sub _read_manualpage_hash() {
|
||||
close($file);
|
||||
}
|
||||
|
||||
sub ServiceStatus() {
|
||||
my $services = shift;
|
||||
my %services = %{ $services };
|
||||
|
||||
# Write the table header
|
||||
print <<EOF;
|
||||
<table class="tbl">
|
||||
<!-- <thead>
|
||||
<tr>
|
||||
<th>
|
||||
$Lang::tr{'service'}
|
||||
</th>
|
||||
|
||||
<th>
|
||||
$Lang::tr{'status'}
|
||||
</th>
|
||||
|
||||
<th>
|
||||
$Lang::tr{'memory'}
|
||||
</th>
|
||||
</tr>
|
||||
</thead> -->
|
||||
|
||||
<tbody>
|
||||
EOF
|
||||
|
||||
foreach my $service (sort keys %services) {
|
||||
my %config = %{ $services{$service} };
|
||||
|
||||
my $pidfile = $config{"pidfile"};
|
||||
my $process = $config{"process"};
|
||||
|
||||
# Collect all pids
|
||||
my @pids = ();
|
||||
|
||||
# Read the PID file or go search...
|
||||
if (defined $pidfile) {
|
||||
@pids = &General::read_pids("${pidfile}");
|
||||
} else {
|
||||
@pids = &General::find_pids("${process}");
|
||||
}
|
||||
|
||||
# Get memory consumption
|
||||
my $mem = &General::get_memory_consumption(@pids);
|
||||
|
||||
print <<EOF;
|
||||
<tr>
|
||||
<th scope="row">
|
||||
$service
|
||||
</th>
|
||||
EOF
|
||||
|
||||
# Running?
|
||||
if (scalar @pids) {
|
||||
# Format memory
|
||||
$mem = &General::formatBytes($mem);
|
||||
|
||||
print <<EOF;
|
||||
<td class="status is-running">
|
||||
$Lang::tr{'running'}
|
||||
</td>
|
||||
|
||||
<td class="text-right">
|
||||
${mem}
|
||||
</td>
|
||||
EOF
|
||||
|
||||
# Not Running
|
||||
} else {
|
||||
print <<EOF;
|
||||
<td class="status is-stopped" colspan="2">
|
||||
$Lang::tr{'stopped'}
|
||||
</td>
|
||||
EOF
|
||||
}
|
||||
|
||||
print <<EOF;
|
||||
</tr>
|
||||
EOF
|
||||
|
||||
}
|
||||
|
||||
print <<EOF;
|
||||
</tbody>
|
||||
</table>
|
||||
EOF
|
||||
}
|
||||
|
||||
1; # End of package "Header"
|
||||
|
||||
Reference in New Issue
Block a user