mikefield
Goto Top

Onlinestatus vom Server im Intranet per html darstellen.

rotes gif bei offline und grünes gif bei online

Hallo Zusammen!

Ich möchte den Status von diversen Endpunkten (Drucker, WS, Server) im lokalen Netz in meinem Intranet einbinden. Leider habe ich keine Ahnung wie man das realisiert. Ob das mit html direkt geht weis ich nicht. PHP habe ich nicht, aber ein Perl Script kann ich einbinden, da ActivePerl installiert ist.

Vielen Dank im voraus
mf

Content-Key: 149819

Url: https://administrator.de/contentid/149819

Ausgedruckt am: 28.03.2024 um 15:03 Uhr

Mitglied: 60730
60730 26.08.2010, aktualisiert am 18.10.2012 um 18:43:16 Uhr
Goto Top
Moin,

eigentlich ein klassischer Fall von Nagios - aber vielleicht für deinen Wunsch etwas oversized.
Try this
Und wenn dir das nicht reicht, eine nähere Angabe zu deiner Intrastruktur würde helfen.

Gruß
Mitglied: noodyn
noodyn 27.08.2010 um 07:13:11 Uhr
Goto Top
Diverse Endpunkte? Dann Nagios.
Mitglied: jamie87
jamie87 27.08.2010 um 11:31:34 Uhr
Goto Top
Eine günstige Alternative wäre vielelicht auch noch Servers Alive. Gibt es sogar eine kostenlose Variante mit bis zu 10 Servern.

Gruß Jamie
Mitglied: Mikefield
Mikefield 27.08.2010 um 14:51:33 Uhr
Goto Top
Erst einmal vielen Dank für die Antworten. Aber ich wollte nicht mit Kanonen auf Spatzen schießen. Es geht nur um mein privates LAN mit 5 Rechnern und einem Drucker. Ich bin davon ausgegangen, daß es ein einfaches script gibt, mit dem man das realisieren kann, ob ein Rechner per ping erreichbar ist und dieses auf meiner Homepage entsprechend darstellen. Ich habe schon ein PERL Script, welches den Zustand der Powerswitche überwacht. Vielleicht kann man das auch anpassen.


#!/usr/bin/perl
#
# $Id: epc_control.pl,v 1.23 2006/11/13 09:50:34 martinb1 Exp $
#
###############################################################################
#                                                                             #
# CommandLine Tool to access the ExpertPowerControl                           #
#                                                                             #
# sample calls:                                                               #
#                                                                             #
#    Show current State:                                                      #
#       - epc_control.pl --host=192.168.0.2 --password=xyz                    #
#                                                                             #
#    Switch On:                                                               #
#       - epc_control.pl --host=192.168.0.2 --password=xyz -on                #
#                                                                             #
#    Switch Off:                                                              #
#       - epc_control.pl --host=192.168.0.2 --password=xyz -off               #
#                                                                             #
#    Toggle:                                                                  #
#       - epc_control.pl --host=192.168.0.2 --password=xyz -t                 #
#                                                                             #
###############################################################################
#                                                                             #
# (c)2004, Author: Martin Bachem, Gude Analog- und Digitalsysteme GmbH        #
# 	http://www.gude.info/                                                 #
#                                                                             #
###############################################################################
#

$adminname = "admin";  
$username  = "user";  
$password = "";  


###############################################################################
# Command Line Parameters Defaults
#
$powerswitch=1;
$action=0;         # 1=on, 2=off, 3=toggle

###############################################################################
# gloval vars
$num_ports=0;
$switch_all=0;
$host = "192.168.150.16";  

$delay=2; # 2 seconds delay when switching all ports
$batch=0; # idle T
$batch_t = 0; # default: time in seconds

sub usage {
	print ("epc_control.pl\n");  
	print ("  [--host=192.168.0.2]       : define the IP-Adress of your device)\n");  
	print ("  [--password=mypasswd]      : define HTTP Password if needed\n");  
	print ("  [--port=X],[-p=X]          : select switch port no. X (default: 1)\n");  
	print ("  [--switch_on], [-on]       : switch PowerSwitch X ON\n");  
	print ("  [--switch_off],[-off]      : switch PowerSwitch X OFF\n");  
	print ("  [--switch_toggle],[-t]     : toggle PowerSwitch X from ON<->OFF\n");  
	print ("  [--batch_onoff=T],[-bon=T] : switch port on, after T seconds switch off");  
	print ("  [--batch_offon=T],[-bof=T] : switch port off, after T seconds switch on");  
	print ("                                  T: 1=1s,  2=2s,  3=3s,  4=4s, 5=5s,");  
	print ("                                     6=10s, 7=15s, 8=20s, 9=30s");  
	print ("  [--batch_min], [-bm]       : handle values for T (see above) as minute values");  
	print ("  [--all,[-a]                : switch all available ports\n");  
	print ("  [--delay=X,[-d=X]          : when --all, delay X seconds after\n");  
	print ("                               each port (default: 2s)\n");  
	print ("  [--silent],[-s]            : no console output\n");  
	exit;
}

foreach $cmd_line_param (@ARGV) {

	# --host=192.168.0.2
#	if ($cmd_line_param =~ "--host=") {  
#		@param_vals = split(/=/, $cmd_line_param);
#		if ($param_vals[1] ne "") {  
#			$host = $param_vals[1];
#		}
#	}

	# --password=mypasswd
	if ($cmd_line_param =~ "--password=") {  
		@param_vals = split(/=/, $cmd_line_param);
		if ($param_vals[1] ne "") {  
			$password = $param_vals[1];
		}
	}

	# [--powerswitch=X], [-p=X]
	if (($cmd_line_param =~ "--powerswitch=")  || ($cmd_line_param =~ "-p=")) {  
		@param_vals = split(/=/, $cmd_line_param);
		if ($param_vals[1] ne "") {  
			$powerswitch = $param_vals[1];
		}
	}

	# [--switch_on], [-on]  --> action = 1;
	if (($cmd_line_param eq "--switch_on") || ($cmd_line_param eq "-on")) {  
		$action = 1;		
	}

	# [--switch_off], [-off]  --> action = 2;
	if (($cmd_line_param eq "--switch_off") || ($cmd_line_param eq "-off")) {  
		$action = 2;		
	}

	# [--switch_toggle], [-t]  --> action = 3;
	if (($cmd_line_param eq "--switch_toggle") || ($cmd_line_param eq "-t")) {  
		$action = 3;		
	}

	# [--all] : do action for all ports (default delay is 2 seconds, modify with --delay)
	if (($cmd_line_param eq "--all") || ($cmd_line_param eq "-a")) {  
		$switch_all = 1;
	}


	# [--batch_onoff=T], [-bon=X] : reset port (switch on, wait, switch off)
	if (($cmd_line_param =~ "--batch_onoff=")  || ($cmd_line_param =~ "-bon=")) {  
		@param_vals = split(/=/, $cmd_line_param);
		if ($param_vals[1] ne "") {  
			$action = 4;
			$batch = $param_vals[1];
		}
	}
	
	# [--batch_onoff=T], [-bon=X] : reset port (switch on, wait, switch off)
	if (($cmd_line_param =~ "--batch_offon=")  || ($cmd_line_param =~ "-bof=")) {  
		@param_vals = split(/=/, $cmd_line_param);
		if ($param_vals[1] ne "") {  
			$action = 5;
			$batch = $param_vals[1];
		}
	}
	

	# [--batch_min], [-bm] : no console output
	if (($cmd_line_param eq "--batch_min") || ($cmd_line_param eq "-bm")) {  
		$batch_t = 1;
	}	

	# [--delay=X], [-d=X] : delay in seconds for allon / alloff / alltoggle
	if (($cmd_line_param =~ "--delay=")  || ($cmd_line_param =~ "-d=")) {  
		@param_vals = split(/=/, $cmd_line_param);
		if ($param_vals[1] ne "") {  
			$delay = $param_vals[1];
		}
	}

	# [--silent], [-s] : no console output
	if (($cmd_line_param eq "--silent") || ($cmd_line_param eq "-s")) {  
		$silent_mode = 1;
	}

	# [--help], [-h]  --> print usage;
	if (($cmd_line_param eq "--help") || ($cmd_line_param eq "-h")) {  
		usage();
	}	
}

###############################################################################
# Include Perl HTTP module
#
eval("use LWP::UserAgent");  
if ($@) {
	print "ERROR: Module LWP::UserAgent not on found this host\n";  
	exit;
}

if (!($host =~ ":"))  {  
	$host = $host.':80';  
}
$full_url = "http://".$host."/";  
$state_txt{"1"} = "ON";  
$state_txt{"0"} = "OFF";  

###############################################################################
# Support Sub-Routines
#
 sub print_current_state {
	if ($silent_mode == 0) {
		for ($i=0; $i<$num_ports; $i++) {
			print "Content-type: text/html\n\n";  
#			print ($port_names[$i]);
#			print (" is ");  
			print ($state_txt{$port_states[$i]});	
			print ("\n");  
                       
		}
	}
 }


# $stat = ($state_txt{$port_states[$i]});
# print "$stat";  
# print "$host";  

# sub print_test {
# print "Content-type: text/html\n\n";  
# print "Die IP-Adresse ist: <b>$host</b>, der Status ist ";  
# print ($state_txt{$port_states[$i]});
# }


sub get_mode {
	$response = $browser->get($full_url);
	@lines = split(/\n/, $response->content);
	
	$num_ports = 0;
	
	foreach $line (@lines) {
		if ($line =~ "powerstate") {  
			@params = split(/\"/, $line);  
			if ($params[3] =~ ",") {  
				
				@current_state = split(/,/, $params[3]);
				$port_names[$num_ports] = $current_state;
				$port_states[$num_ports] = $current_state[1];
				
				$num_ports = $num_ports+1;
			}
		}
	}
	
	return ($num_ports > 0);
}

sub login {
	my $login_ok = 0;

	# just login 
	if ($action == 0) {
		$response = $browser->get($full_url);
	} else {
		if ($num_ports == 1) {
			$response = $browser->get($full_url."switch.html");  
		} else {
			$response = $browser->get($full_url."ov.html");  
		}
	}
	
	# Login successfull...	
	if ($response->is_success) {
		$login_ok = 1;
		@lines = split(/\n/, $response->content);
		foreach $line (@lines) {
			if ($line =~ "blocked") {  
				if ($silent_mode == 0) {
					print ("ACCESS DENIED! $line\n");  
				}
					print ("ACCESS DENIED! $line\n");  
				$login_ok = 0;
			}
		}
	}
	
	return ($login_ok);
}


sub switch_action() {
	# Toggle ?
	if ($action == 3) { 
		# print ("$port_names[$powerswitch-1]\n");  
		if ($port_states[$powerswitch - 1] == 0) {
			$action = 1;
		} else {
			$action = 2;
		}
	}

	# Switch ON ?		
	if ($action == 1) { 
		$response = $browser->get($full_url."SWITCH.CGI?s".$powerswitch."=1");  
	}

	# Switch OFF ?
	if ($action == 2) { 
		$response = $browser->get($full_url."SWITCH.CGI?s".$powerswitch."=0");  
	}

	# batchmode on->wait->off
	if ($action == 4) { 
		if ($num_ports == 1) {
			$response = $browser->get($full_url."RESET.CGI?a=1&bi=".$batch."&t=".$batch_t."&b=0");  
		} else {
			$response = $browser->get($full_url."RESET.CGI?a=1&bi=".$batch."&t=".$batch_t."&b=0&p=".$powerswitch);  
		}
	}
	
	# batchmode off->wait->on
	if ($action == 5) { 
		if ($num_ports == 1) {
			$response = $browser->get($full_url."RESET.CGI?a=0&bi=".$batch."&t=".$batch_t."&b=1");  
		} else {
			$response = $browser->get($full_url."RESET.CGI?a=0&bi=".$batch."&t=".$batch_t."&b=1&p=".$powerswitch);  
		}
	}
	
}

################################################################################################
# MAIN APP
#

if ($host eq "") {  
	print ("\nparameter --host is missing! try 'epc_control.pl --host=192.168.x.x'\n");  
	print ("or 'epc_control.pl --help\n\n");  
	exit;
}

$browser = LWP::UserAgent->new;
$browser->credentials(
    $host,
    'ExpertPowerControl',  
    $adminname => $password
);
$browser->agent($ENV{SCRIPT_NAME});  

if (get_mode()) {
	if (login()) {

		if ($switch_all) {
			if ($delay < 1) {
				print ("ERROR: delay musst be >= 1s");  
				exit;
			}
			
			for ($i=0; $i<$num_ports; $i++) {
				$powerswitch = $i+1;
				$save_action = $action;
				switch_action();
				$action = $save_action;
				sleep($delay);
			}
			print ("\n");  
						
		} else {
			switch_action();
		}

		get_mode();
		print_current_state;

# test;

# current_state;
		
		# do Logout
		$response = $browser->get($full_url);
		
	} else {
		print "ERROR: " . $response->code. " " . $response->message; 	  
		if ($response->code eq 401) {
			print ("\ntry \"epc_control.pl --password=YourPassword\"\n");  
		}
	}
}	else {
	printf ("Could not connect $full_url !");  
}

# print ($state_txt{$port_states[$i]});


mf