Monitoring Samba from a web page using smbstatus

A long time ago, I found that I needed something to be able to see which users are logged in using samba and kick users as necessary. The usual way is to check the currently logged in users using smbstatus and kicking users by killing the smb process belonging to a user. To make this a bit easier, I made a small webpage in *cough*PHP*cough*. Hey, it was 2 years ago! šŸ™‚ Anyway, I still find it very useful, so maybe it’s useful to you, too.

<?
error_reporting(0);

if(isset($_GET&#91;'kill'&#93;)){
	echo shell_exec("sudo /scripts/smbkill ".$_GET&#91;'kill'&#93;." 2>&1");
	}

exec("smbstatus -S",$log);
exec("smbstatus -L",$lock);

$pid_us = array();
for($i=3;$i<sizeof($log);$i++)
	if(!trim($log&#91;$i&#93;)=="") {
		$users&#91;&#93; = split("&#91; &#93;+",$log&#91;$i&#93;,4);
		$pid_us&#91;$users&#91;sizeof($users)-1&#93;&#91;1&#93;&#93; = $users&#91;sizeof($users)-1&#93;&#91;2&#93;;  
		}
for($i=3;$i<sizeof($lock);$i++)
	if(!trim($lock&#91;$i&#93;)=="") {			
			$line = split("&#91; &#93;+",$lock&#91;$i&#93;,7);
			$usr = $pid_us&#91;$line&#91;0&#93;&#93;;
			if(trim($usr)=="") $usr = $line&#91;0&#93;;									
			$locks&#91;$line&#91;0&#93;&#93;&#91;&#93; = array($line&#91;0&#93;,$usr,substr_replace($line&#91;6&#93;,"",-25));	//Remove date (25 chars from the right
		}
	
//die("");

echo "<br />";
echo "<font style=\"font-family: Verdana;\">Connected users:</font>\n";
echo "<br /><br />";
echo "<table>\n";
foreach($users as $user){
	list($service,$pid,$mach,$date) = $user;
	echo "<tr\n>";
	echo "<td><a onclick=\"return confirm('Kick this user?');\" href=\"".$PHP_SELF."?kill=".$pid."\"><img border=\"0\" src=\"remove.gif\" /></a></td>\n";
	echo "<td class=\"user\">".$mach."</td>\n";
	echo "<td class=\"pid\">".$pid."</td>\n";
	echo "<td>".$service."</td>\n";
	echo "<td>".$date."</td>\n";
	echo "</tr>\n";
}
echo "</table>\n\n";

echo "<br /><br />";
echo "<font style=\"font-family: Verdana;\">Locked files:</font>\n";
echo "<br /><br />";
echo "<table>\n";

$cuser = "";
$first = true;
foreach($locks as $l){
	foreach($l as $lck){
		list($pid, $user,$file) = $lck;
		if($user != $cuser){		
			if(!$first){
				echo "<tr>\n";
				echo "<td>&nbsp;</td>\n";
				echo "<td>&nbsp;</td>\n";
				echo "<td>&nbsp;</td>\n";
				echo "</tr>\n";				
				}
			echo "<tr>\n";			
			echo "<td class=\"pid\"><a onclick=\"return confirm('Kick this user?');\" href=\"".$PHP_SELF."?kill=".$pid."\"><img border=\"0\" src=\"remove.gif\" /></a> ".$pid."</td>\n";
			echo "<td class=\"user\">".$user."</td>\n";
			echo "<td>".$file."</td>\n";
			echo "</tr>\n";
			$cuser = $user;
			}
		else {
			echo "<tr>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>".$file."</td>\n";	
			echo "</tr>\n";				
			}
		$first = false;
		}
}
echo "</table>\n";
?>

Note that you should of course make sure that only you are able to access the page and execute the kill command, so you’ll need to get creative with sudo and htaccess šŸ˜‰

Image used: remove

Advertisements

~ by moiristo on December 14, 2008.

8 Responses to “Monitoring Samba from a web page using smbstatus”

  1. Cool…
    This was exactly what I was looking for… šŸ™‚

  2. super
    where can I find smbkill script

  3. smbkill just performs some security measures, it eventually calls ‘kill -9’ on the passed process ID.

  4. Thank you very much! Worked great for us! šŸ™‚

  5. Can you provide the smbkill script

  6. Are you serious? This is one VERY BIG security thread to your server =D

  7. It depends on how you use it; you should have authorization on the status page and you should make sure that the kill script is secure as well. I didn’t go into this as security was not such a big issue in my case, but it’s as secure as you make it. Anyway, I think the script’s still useful for monitoring without the kicking functionality, so you could leave that out.

  8. Thank you for this example. I based myself on this one to generate a file xml.
    And I created a webpage in jquery mobile using this xml file.

    You can find my work on github : https://github.com/35niavlys/smbstatus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: