]> cvs.zerfleddert.de Git - rsbs2/blobdiff - rsbs2.pl
use unified write_file function everywhere...
[rsbs2] / rsbs2.pl
index 5255b58e87d76c930c9369555b29501f7db3c68a..0843330fb25edf6c3397d5015d5fb92172ac3d94 100755 (executable)
--- a/rsbs2.pl
+++ b/rsbs2.pl
@@ -9,10 +9,13 @@ use Digest::MD5 qw(md5);
 my $ua = LWP::UserAgent->new;
 my $sid;
 my $host;
+my $poweronms=200;
+my $poweroffms=5000;
+my $verbose = 0;
 
 my @fw_vars = qw(ENABLE_LAN_AUTONEG ENABLE_LAN_100 ENABLE_LAN_FDUPLEX GATEWAY
        IP_ADDRESS NETMASK TFTP_FIRMWARE_FILE TFTP_ADDR_FIRMWARE ENABLE_DHCP
-       ENABLE_DNS DNS_SERVER DNS_DOMAIN_NAME DHCP_CONFIGURE_DNS
+       ENABLE_DNS DNS_SERVER DNS_DOMAIN_NAME
        ACCESS_CONTROL_SERVER_1 ACCESS_CONTROL_SERVER_2 ACCESS_CONTROL_SERVERS
        ACPI_DISABLE_BIOS_SCAN ACPI_FORCE_RSDP_ADDRESS ACPI_FORCE_RSDT_ADDRESS
        ACPI_RSDP_BIOS_ROM_ADDRESS ACPI_SCAN_DELAY_SECONDS AMR_AUTH_METHOD
@@ -32,10 +35,10 @@ my @fw_vars = qw(ENABLE_LAN_AUTONEG ENABLE_LAN_100 ENABLE_LAN_FDUPLEX GATEWAY
        CONSOLE_VIDEO_PARAM_MODE CONTACT CONTACT_PHONE CRIT_TEMP_SHUTDOWN
        CRIT_VOLT_SHUTDOWN DHCP_ADD_EXTENSION DHCP_ADD_SERIAL
        DHCP_CONFIGURE_DNS DHCP_HOSTNAME_EXT DHCP_SERVER DHCP_USE_CARDNAME
-       DIAG_URL DNS_DOMAIN_NAME DNS_SERVER ENABLE_ANON_IPMI ENABLE_ANON_PCI
+       DIAG_URL ENABLE_ANON_IPMI ENABLE_ANON_PCI
        ENABLE_ANON_WEB ENABLE_AVR_CHIP_DETECT ENABLE_BMC_AUTODETECT
        ENABLE_BMC_TIMESYNC ENABLE_CRTC_FETCH ENABLE_DHCP ENABLE_DHCP_HOSTNAME
-       ENABLE_DNS ENABLE_DS_CONNECTIVITY ENABLE_IO_UART_DECODER ENABLE_LAN_100
+       ENABLE_DS_CONNECTIVITY ENABLE_IO_UART_DECODER ENABLE_LAN_100
        ENABLE_LAN_AUTONEG ENABLE_LAN_FDUPLEX ENABLE_MEM_UART_DECODER
        ENABLE_PPP ENABLE_REMOTE_FLOPPY_BOOT ENABLE_SELF_DELETE
        ENABLE_SERIAL_DBG ETHDRIVER_SID EXPROM_BANNER EXPROM_EBDA_COMPATIBILITY
@@ -156,6 +159,8 @@ sub _getprop {
 
        my $reqstr='<?xml version="1.0"?><?RMCXML version="1.0"?><RMCSEQ><REQ CMD="propget"><PROPLIST><PROP NAME="'.$property.'"/></PROPLIST></REQ></RMCSEQ>';
        my $resp = _req($reqstr);
+
+       print "get: ${property}\n" if ($verbose);
        
        if ($resp->{RESP}->{RC} ne '0x0') {
                $resp->{RESP}->{RC};
@@ -165,7 +170,7 @@ sub _getprop {
 }
 
 sub logout {
-       print "Logout\n";
+       print "Logout\n" if ($verbose);
        my $request = HTTP::Request->new(GET => "http://${host}/cgi/logout");
        $request->header(Cookie => "sid=$sid");
        my $response = $ua->request($request);
@@ -182,7 +187,7 @@ sub setprop {
        my $oldval = _getprop($property)->{RESP}->{PROPLIST}->{PROP}->{VAL};
 
        if ($value eq $oldval) {
-               print "${property} is already ${value}\n";
+               print "${property} is already ${value}\n" if ($verbose);
                return;
        }
        
@@ -199,7 +204,15 @@ sub setprop {
 sub serveraction {
        my $action = shift;
 
-       print "${action}...\n";
+       my $pmode = 2;
+
+       setprop("SERVER_HARD_RESET_VIA_IPMI", "FALSE");
+       setprop("SERVER_POWER_CHANGE_VIA_IPMI", "FALSE");
+       #PM Mode
+       setprop("SERVER_POWER_ON_MODE", sprintf("0x%x", $pmode));
+       setprop("SERVER_POWER_OFF_MODE", sprintf("0x%x", $pmode));
+
+       print "${action}...\n" if ($verbose);
        my $reqstr='<?xml version="1.0"?><?RMCXML version="1.0"?><RMCSEQ><REQ CMD="serveraction"><ACT>'.$action.'</ACT></REQ></RMCSEQ>';
        my $res = _req($reqstr);
 
@@ -208,6 +221,27 @@ sub serveraction {
        }
 }
 
+sub powerup {
+       print "powerup\n" if ($verbose);
+       setprop("SERVER_POWER_ON_PULSE_MS", sprintf("0x%x", $poweronms));
+       setprop("SERVER_POWER_OFF_PULSE_MS", "0x0");
+       serveraction("powercycle");
+}
+
+sub powerdown {
+       print "powerdown\n" if ($verbose);
+       setprop("SERVER_POWER_ON_PULSE_MS", "0x0");
+       setprop("SERVER_POWER_OFF_PULSE_MS", sprintf("0x%x", $poweroffms));
+       serveraction("powercycle");
+}
+
+sub powercycle {
+       print "powercycle\n" if ($verbose);
+       setprop("SERVER_POWER_ON_PULSE_MS", sprintf("0x%x", $poweronms));
+       setprop("SERVER_POWER_OFF_PULSE_MS", sprintf("0x%x", $poweroffms));
+       serveraction("powercycle");
+}
+
 sub showprop {
        my $property = shift;
 
@@ -274,16 +308,16 @@ sub login {
        my $xmlin = XMLin($response->decoded_content);
        die "Error getting Challenge: ".$xmlin->{RC} if ($xmlin->{RC} ne '0x0');
        my $challenge = $xmlin->{CHALLENGE};
-       print "Challenge: ${challenge}\n";
+       print "Challenge: ${challenge}\n" if ($verbose);
 
        $sid = $response->headers->header('Set-Cookie');
        die "No SessionID!" if (!defined($sid));
        chomp($sid);
        $sid =~ s/.*sid=(.*);.*/$1/;
-       print "SID: ${sid}\n";
+       print "SID: ${sid}\n" if ($verbose);
 
        my $login_hash = _hash($pass, $challenge);
-       print "Hash: ${login_hash}\n";
+       print "Hash: ${login_hash}\n" if ($verbose);
 
        my $request = HTTP::Request->new(GET => "http://${host}/cgi/login?user=${user}&hash=${login_hash}");
        $request->header(Cookie => "sid=$sid");
@@ -317,53 +351,195 @@ while(<INIFILE>) {
 }
 close(INIFILE);
 
-$host = ${$Config{$sections[0]}}{"host"};
+my $valid_arg = 0;
+my $powup = 0;
+my $powdown = 0;
+my $powcyc = 0;
+my $reset = 0;
+my @sprop = ();
+my @gprop = ();
+my $show = 0;
+my $enable_debug = "";
+my $save = "";
+my $load = "";
+my $hostalias;
+
+while (defined($ARGV[0])) {
+       SWITCH: for ($ARGV[0]) {
+               /^-v$/  && do {
+                               $verbose = 1;
+                               shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-g$/ && do {
+                               shift @ARGV;
+                               push @gprop, shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-s$/ && do {
+                               shift @ARGV;
+                               push @sprop, shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-u$/ && do {
+                               $powup = 1;
+                               shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-d$/ && do {
+                               $powdown = 1;
+                               shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-c$/ && do {
+                               $powcyc = 1;
+                               shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-r$/ && do {
+                               $reset = 1;
+                               shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-l$/ && do {
+                               shift @ARGV;
+                               $enable_debug = shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-x$/ && do {
+                               $show = 1;
+                               shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-save$/ && do {
+                               shift @ARGV;
+                               $save = shift @ARGV;
+                               last SWITCH;
+                       };
+               /^-load$/ && do {
+                               shift @ARGV;
+                               $load = shift @ARGV;
+                               last SWITCH;
+                       };
+
+               if (defined($ARGV[0])) {
+                       $hostalias = $ARGV[0];
+                       shift(@ARGV);
+                       $valid_arg = 1;
+               }
 
-$sid = login(${$Config{$sections[0]}}{"user"}, ${$Config{$sections[0]}}{"pass"});
+               while ( defined($ARGV[0]) ) { $valid_arg = 0; shift(@ARGV); }
+       }
+}
 
-show_boarddesc();
+if ($valid_arg && (!defined(${$Config{$hostalias}}{'host'}))) {
+       $valid_arg = 0;
+}
 
-#showprop("FP_REMOTE_POWER");
-#showprop("FP_REMOTE_BOOT");
-#showprop("SERVER_HARD_RESET_VIA_IPMI");
-#showprop("SERVER_HARD_RESET_PULSE_MS");
-#showprop("SERVER_POWER_CHANGE_VIA_IPMI");
-#showprop("SERVER_POWER_ON_MODE");
-#showprop("SERVER_POWER_ON_PULSE_MS");
-#showprop("SERVER_POWER_OFF_MODE");
-#showprop("SERVER_POWER_OFF_PULSE_MS");
+if (!$valid_arg) {
+       print STDERR "Usage: $0 options card-alias\n";
+       print STDERR "Options:\n";
+       print STDERR "\t-g property\tget property value\n";
+       print STDERR "\t-s property=val\tset property value\n";
+       print STDERR "\t-u\t\tpowerup\n";
+       print STDERR "\t-d\t\tpowerdown\n";
+       print STDERR "\t-c\t\tpowercycle\n";
+       print STDERR "\t-r\t\treset\n";
+       print STDERR "\t-x\t\tshow all properties, variables and settings\n";
+       print STDERR "\t-l IP\t\tsend SYSLOG debug messages to IP\n";
+       print STDERR "\t-v\t\tverbose\n";
+       print STDERR "\t-save file\tsave configuration to 'file'\n";
+       print STDERR "\t-load file\tload configuration from 'file'\n";
+       print STDERR "\n";
+       print STDERR "card-alias\tone of: ";
+       foreach my $alias (@sections) {
+               print STDERR "\"${alias}\" ";
+       }
+       print STDERR "(see ~/.rsbs2rc)\n";
+       exit(1);
+}
 
-board_properties();
+$host = ${$Config{$hostalias}}{"host"};
+$poweronms = ${$Config{$hostalias}}{"poweronms"} if (defined(${$Config{$hostalias}}{"poweronms"}));
+$poweroffms = ${$Config{$hostalias}}{"poweroffms"} if (defined(${$Config{$hostalias}}{"poweroffms"}));
 
-#show_all_vars();
+$sid = login(${$Config{$hostalias}}{"user"}, ${$Config{$hostalias}}{"pass"});
 
-syslog_debug("255.255.255.255", "TRUE");
+if ($show) {
+       show_boarddesc();
+       board_properties();
+       show_all_vars();
+}
 
-#server_power_on modes: (com/agilent/rmc/mgui/panels/PowerMgmtConf.class)
-#0: l_pmconf_option_disabled
-#1: l_pmconf_option_atx
-#2: l_pmconf_option_relay
-#default: disabled
+if (@gprop) {
+       foreach my $p (@gprop) {
+               showprop($p);
+       }
+}
+
+if (@sprop) {
+       foreach my $p (@sprop) {
+               (my $pr, $v) = split(/=/,$p,2);
+               setprop($pr, $v);
+       }
+}
+
+if ($save ne '') {
+       my @dontsave = qw(ENABLE_LAN_AUTONEG ENABLE_LAN_100 ENABLE_LAN_FDUPLEX
+       GATEWAY IP_ADDRESS NETMASK TFTP_FIRMWARE_FILE TFTP_ADDR_FIRMWARE
+       ENABLE_DHCP HELP_LOCATION HELP_LOCATION PPP_PORT PPP2_INIT PPP2_PORT
+       SERVER_ID SNMP_SERVER_1 SNMP_SERVER_2 SNMP_SERVER_3 SNMP_SERVER_4
+       SNMP_SERVER_5 SNMP_SERVER_6 SNMP_SERVER_7 SERVER_AD_NAME
+       SERVER_AD_NAME2 SERVER_AD_NAME3 SERVER_AD_NAME4 SERVER_MAC_ADDRESS
+       SERVER_MAC_ADDRESS2 SERVER_MAC_ADDRESS3 SERVER_MAC_ADDRESS4
+       MAC_ADDRESS LAST_CARD_NAME LAST_ENABLE_DHCP LAST_GATEWAY
+       LAST_IP_ADDRESS LAST_NETMASK);
+
+       open (SAVEFILE, ">${save}") || die "Error opening save-file: $!\n";
+       foreach my $ts (@fw_vars) {
+               next if (grep(/^${ts}$/, @dontsave));
+
+               my $phash = _getprop($ts)->{RESP}->{PROPLIST}->{PROP};
+               next if ($phash->{PERMS} ne 'RW');
+               
+               print SAVEFILE "${ts}=".$phash->{VAL}."\n";
+       }
+       close(SAVEFILE);
+}
+
+if ($load ne '') {
+       open (LOADFILE, "<${load}") || die "Error opening load-file: $!\n";
+       while(<LOADFILE>) {
+               chomp;
+               (my $p, my $v) = split(/=/, $_);
+               setprop($p, $v);
+       }
+       close(LOADFILE);
+       print "Settings loaded, resetting board...\n";
+       my $reqstr='<?XML version="1.0"?><?RMCXML version="1.0"?><RMCSEQ><REQ CMD="boardreset"></REQ></RMCSEQ>';
+       _req($reqstr);
+       exit(0);
+}
+
+if ($enable_debug ne '') {
+       syslog_debug($enable_debug, "TRUE");
+}
 
-my $pmode = 2;
+if ($reset) {
+       print "hardreset\n" if ($verbose);
+       serveraction("hardreset");
+}
 
-setprop("SERVER_HARD_RESET_VIA_IPMI", "FALSE");
-setprop("SERVER_POWER_CHANGE_VIA_IPMI", "FALSE");
-#Power Mgmt. Pane
-setprop("FP_REMOTE_POWER", "TRUE");
-#PM Mode
-setprop("SERVER_POWER_ON_MODE", sprintf("0x%x", $pmode));
-setprop("SERVER_POWER_OFF_MODE", sprintf("0x%x", $pmode));
-#Hack: Abuse powercycloe for powerup and down...
-#setprop("SERVER_POWER_ON_PULSE_MS", "0x0");
+if ($powup) {
+       powerup();
+}
 
-#$reqstr='<?xml version="1.0"?><?RMCXML version="1.0"?><RMCSEQ><REQ CMD="boardpropset"><BPROP NAME="BOARD_DESCRIPTION"><VAL>'.$boarddesc_new.'</VAL></BPROP></REQ></RMCSEQ>';
-#print $reqstr."\n";
-#print Dumper(_req($reqstr));
+if ($powdown) {
+       powerdown();
+}
 
-serveraction("hardreset");
-serveraction("powerup");
-serveraction("powerdown");
-serveraction("powercycle");
+if ($powcyc) {
+       powercycle();
+}
 
 logout();
Impressum, Datenschutz