Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for checking if binary process is running #56

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/esm.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@
},
{
"name": "SSH",
"binary": "sshd",
"host": "localhost",
"port": 22,
"protocol": "tcp"
},
{
"name": "Init Service",
"binary": "init"
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions js/esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ esm.getServices = function() {
html += '<tr>';
html += '<td class="w15p"><span class="label '+label_color+'">'+label_status+'</span></td>';
html += '<td>'+data[line].name+'</td>';
html += '<td class="w15p">'+data[line].port+'</td>';
html += '<td>'+data[line].detail+'</td>';
html += '</tr>';

$box.append(html);
Expand Down Expand Up @@ -368,4 +368,4 @@ esm.mapping = {
network: esm.getNetwork,
ping: esm.getPing,
services: esm.getServices
};
};
37 changes: 35 additions & 2 deletions libs/Utils/Misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public static function whichCommand($cmds, $args = '', $returnWithArgs = true)
return $return;
}


/**
* Allows to pluralize a word based on a number
* Ex : echo 'mot'.Misc::pluralize(5); ==> prints mots
Expand All @@ -156,6 +155,7 @@ public static function pluralize($nb, $plural = 's', $singular = '')
}



/**
* Checks if a port is open (TCP or UPD)
*
Expand Down Expand Up @@ -207,4 +207,37 @@ public static function scanPort($host, $port, $protocol = 'tcp', $timeout = 3)

return false;
}
}

/**
* Checks if we are able to test for running processes on this OS
*
* @return bool True if the available else false
*/
public static function checkIfProcessRunningAvailable()
{
if (is_executable("/bin/pidof"))
{
return true;
}

return false;
}

/**
* Checks if a process is running (using /bin/pidof)
*
* @param string $process Process name to check
* @return bool True if the process is running else false
*/
public static function checkIfProcessRunning($process)
{
exec("/bin/pidof $process", $response);

if ($response)
{
return true;
}

return false;
}
}
87 changes: 77 additions & 10 deletions libs/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,94 @@

$show_port = $Config->get('services:show_port');

$check_binary_allowed = Misc::checkIfProcessRunningAvailable();

if (count($Config->get('services:list')) > 0)
{
foreach ($Config->get('services:list') as $service)
{
$host = $service['host'];
$port = $service['port'];
$name = $service['name'];
$binary = $service['binary'];
$protocol = isset($service['protocol']) && in_array($service['protocol'], $available_protocols) ? $service['protocol'] : 'tcp';

if (Misc::scanPort($host, $port, $protocol))
$check_port = false;
$check_binary = false;

if ( (isset($host)) && (isset($port)) && (isset($protocol)) )
{
$check_port = true;
}

if ($check_binary_allowed === true)
{
if (isset($binary))
{
$check_binary = true;
}
}

$port_status = 1;
$binary_status = 1;
$status = 0;

if ($check_port === true)
{
if (Misc::scanPort($host, $port, $protocol))
$port_status = 1;
else
$port_status = 0;
}

if ($check_binary === true)
{
if (Misc::checkIfProcessRunning($binary))
$binary_status = 1;
else
$binary_status = 0;
}

if ( ($port_status > 0) && ($binary_status > 0) )
{
$status = 1;
else
$status = 0;

$datas[] = array(
'port' => $show_port === true ? $port : '',
'name' => $name,
'status' => $status,
);
}

$detail = "";

if ( ($check_port === true) && ($check_binary === true) )
{
if($show_port === true)
{
$detail = $protocol . ":" . $port . " and " . $binary;
}
else
{
$detail = $binary;
}
}
elseif($check_port === true)
{
if($show_port === true)
{
$detail = $protocol . ":" . $port;
}
}
elseif($check_binary === true)
{
$detail = $binary;
}

if ( ($check_port === true) || ($check_binary === true) )
{
$datas[] = array(
'detail' => $detail,
'name' => $name,
'status' => $status,
);
}
}
}


echo json_encode($datas);
echo json_encode($datas);