2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-31 01:56:39 +00:00
Bin Xu eea661e405 merge from master to 2.13 branch for 2.13.9 release. (1) (#4525)
* fix issueNode range not specified, see man page for syntax. return with no output when site.master is not set #4299

* Fix issue 4246, record rflash process in log file

* dodiscovery: better disksize - ordered by major,minor and no `\n` (#4219)

* better disksize: ordered by major,minor and no `\n`

* nodediscover:`disksize` sent correctly: no need to `split`,`join`

* general sort with explicit key columns, fix search pattern

* dodiscovery: Fix bugs

* dodiscovery: Fix ShellCheck SC2007,SC2004

* dodiscovery: simpler kcmdline parsing, disksize as IEC binary prefix

* Add test case - switch_to_dns_forward_mode

* Modify timeout of login by curl command for OpenBMC

* Add makegocons command

This patch enable goconserver service and manage the node sessions
with a new `makegocons` command.

Implement: #4215

* Refine the Cumulus Linux Section of the Doc (#4249)

* No content in the switches subdirectory in docs, remove

* Change the Network topic to Networking

* Refine and reformat the Cumulus Linux documentation

* Modify the script to build xCAT-genesis-base package (#4292)

* Verify hash ID

* change log messages

* Timeout the ntpd process if ntpd service is not running on ntpserver (#4321)

* enhance nodediscovery process: don't write "NOIP" if the node can not be resloved to an IP (#3995)

* enhance nodediscovery process, if only 1 mac and have \*NOIP\* append, don't write mac table and don't generate dhcp lease entry

* Enhance PR 3995: enhance nodediscovery process: don't write "NOIP" if the node can not be resloved to an IP

* modified depending on comments

* natural_sort_cmp: recursion to iterative implementation (#4314)

* natural_sort_cmp: recursion to iterative implementation

* natural_sort_cmp: correct prototype with parameters, as per `man perlfunc`

* Task 3339, rspconfig ip/netmask/gateway/vlan support

*   * Minor enhance on xcatperftest to put all logs into one file
  * Fix a bug in simulatorctl.sh, and it cause the script cannot be found

* modified depending on comments

* QA list for makehosts (#4284)

* QA list for makehosts

* Fix some bus in makegocons

* Revise test case - switch_to_dns_forward_mode

* Enhancements after the review

* Use IO::Socket to check BMC console port

* Revise test case - switch_to_dns_forward_mode

* modified depending on comments

* Add messages to inform the user of the general action started via xCAT for flashing firmware

* Fix bug, anything in the functional array is the one that's really active, priority will not be 0 if there is pending firmware

* Fix 4338, remove all of  in child process

* fix issue #4354 :The XCATTEST_CN in xcattest can not detect HCP as config file (#4355)

* Fix #4330, close the socket

* Add image name to /opt/xcat/xcatinfo on compute node (#4359)

* Integrate congo console from goconserver with rcons

Enhance the original implement of rcons to support goconserver.
`rcons` will help the user choose from one of the console backend
service based on the console server status of management node.

Implement #4216

* Fix 4363 - discovery broken (#4364)

* do not set exit_code to 1 when the clock is not synced before the timeout (#4362)

* Fix check for MTM values with spaces

* modified depending on comments

* rspconfig admin_password for OpenBMC

* Fix merging in xdcpmerge.sh (#4328)

* Fixes in xdcpmerge.sh

Two fixes:
1. The grep pattern when finding duplicate usernames is missing ":" at the end. So, for example user "test" would also match "test2, etc.". Adding the ":" delimiter fixes the issue.
2. Another issue happens when the file to be merged is a superset of the files on the nodes. For example, if a new user is added and entire passwd file (that is otherwise identical) is sent to be merged. In this case, the $filebackup.nodups file, i.e. the original file with duplicates removed, becomes empty and the condition "if [ -s "$filebackup.nodups" ]" does not execute. Then the merged file ends up being original file with the merge file fully appended, clearly not what was intended.

This is solved by changing the condition to check for file existence "-a" rather then for size. Additionally, I also turn the logic around so that the duplicates are removed from the merge file and then added to the original file. I think this makes logic a bit cleaner and also ensures that existing entries are not reordered or changed in any way.

* Streamlining previous commit

Adjustment to previous commit, streamlining and simplifying logic. Once $mergefile.nodups is created, just concatenate it the original file.

* Update to xdcpmerge

No need to copy $filebackup to $curfile, they are the same.

* Modify for debug conveniently

* add new cases and delete outdated test cases

* Modify genesis build script for centos x86_64

* Add space between at and the time

* Add a print out of the firmware levels for the various UT cases

* Add unit test cases for rspconfig

* rspconfig fix for set hostname

* Enhance the testing case for rspconfig setting hostname

* modified depending on comments

* modified depending on comments

* Add %pretrans script in <lua>. Handle directory to symlink change properly. See comment #3 of https://bugs.launchpad.net/rpm/+bug/633636

* Make xCAT-genesis-base confliects with early version of xCAT-genesis-scripts

* rspconfig dump to allow admins capture logs

* Adding comment

* Improve the error message when BMC does not return a dump ID

* Improve some messages and add timestamp for downloaded dump file

* Improve the message to help Admin figure out where the file is missing

* Leave a log file there when xCAT upgrade in case to debug issue while upgrading (#4389)

* Listen on 0.0.0.0 instead of the hostname

This patch modify the configuration of `makegocons` and `rcons`
for goconserver.

`cat /etc/goconserver/server.conf`
```
global:
  host: 0.0.0.0
  ssl_key_file: /etc/xcat/cert/server-key.pem
  ssl_cert_file: /etc/xcat/cert/server-cert.pem
  ssl_ca_cert_file: /etc/xcat/cert/ca.pem
  logfile: /var/log/goconserver/server.log
api:
  port: 12429
console:
  port: 12430
```

* Support hostname=* for openbmc

* Relay action and snmp configuration support for Coral PDU

* ddns.pm: specify the "directory" option for DNS slaves too (cf. bug #4392)

* Fix issue 4361, modify some sendmsg to message

* 1. add "makeconserver -d" to "rmdef -C", 2. add "makeconserver -C|--cleanup" to remove entries for removed nodes

* When there is a problem with the login, do not hide the message on debug mode. BMCReady does not make sense if the admin does not know how to find that state

* Change function from login_logout_request to login_request, not doing any logout here

* Check that RC is 200 to prevent unknown issues, handle the response generically

* OpenBMC rspconfig dump timeout fixes

* Fix issue 4408, modify error for rspconfig dump

* Clear all BMC Dump logs when BMC firmware flash

* modified depending on comments

* More modifications for pr 4386, to deal with the conflicts

* To handle one case which have 2 implementations, which one is for specific platform, on is for all platforms

* return when current status is RSPCONFIG_DUMP_DOWNLOAD_REQUEST

* fix issue 4417, delete 'clear next_status'

* fix issue 4353: rspconfig needs to support multiple IPs on the BMC and ignore ZeroConfigIPs

* Wait 15 seconds after OpenBMC interface with vlan tag to be activated

* Fix issue #4397: rspconfig <> hostname=xxx show error message when there is multiple network in bmc

* Some sentence modify for makeconservercf -C|--cleanup

* OpenBMC rspconfig dump better dump file name formatting

* Removed the --check and --ipsource option with PR 4258, update the man page

* Improve the message on the HTTP response

* modified depending on comments

* Only handle 404 and 504 in the login request code, defer the rest to deal_with_response()

* rflash stream support

* 1. configure ip/netmask/gateway only on the NIC whose IP match node BMC attribute, 2. add some information for LinkLocal address

* Fix confignetwork bond nic_type detection with multiple bonds

* Modified configonie --ntp command (#4436)

* Add man page for makegocons

This is the guide about how to make goconserver as
a replacement for conserver to help slove the issues reported
for conserver, like: #4043, #3543. For openbmc, the solution of
goconserver is much light-weighted than the conserver which could
help save the system resource. In addition, sshpass is not needed
for openbmc with goconserver.

Implement: #4337

* Add another key for node_info in order not to after the content of $node_info{$node}{bmc}

* enhance rflash stream

* makedhcp does not work well when all service nodes not running dhcp but disjointdhcps=1 (#4426) (#4440)

- if all service nodes not running dhcp, to treat it as disjointdhcps=0
- nodeset will send request to MN by default even if disjointdhcps=1
- Move out of the dhcp service checking from opts pre-check, and do it just before real makedhcp handling.

* rspconfig configure bmc vlan will hung because of PR 4383

* OpenBMC rspconfig dump enhancements

* Changes due to review comments

* Print debug message before login attempt

* Add warning when xCAT throttles SSL connections

* Display first [openbmc_debug] when entering openbmc.pm

* modified error msg

* Make sure credential files have a trailing newline (#4442)

* modified depending on comments

* Fix the typo in the man page of makegocons

* Update the print out based on the review comment, should not use  since the regular expression is removed

* Modify the nodeset disjoint test case accordingly for #4426

* Use short hostname in rcons for goconserver

As the certificate of xcat is signed with short hostname, this
commit force to use the short hostname in  the environment variable for
`congo console`.

* Fix issue 3497, make sense for reventlog msg

* Give summary after flash active when no debugmode

* Fix the issue that the IP configuration will fail if bmc attribute is a hostname

* enhance genimage for sles12sp2 (#4450)

* Add dhcp-client-identifier to lease block (#4429)

Machines that use Infiniband for PXE booting need to have the
dhcp-client-identifier set in the lease block.
Without it, they will not get the lease from the server.

* Support multiple bonds on bring-up

* modified depending on comments

* fix the check for rc to 1 on error cases

* modify response for bmcdiscover when error

* Ignore syslog error in monitorctrl when setNodeStatusAttributes (#4459)

* fix issue https://github.com/xcat2/xcat-core/issues/4411 (#4462)

* fix issue Compute nodes fail to get provisioned #4411: covert imgsrv and xcatmaster to their ip addresses in case the hostname cannot be resolved inside initrd for diskless

* More strict check to tell if it is a chroot env to avoid modify DB (#4463)

when genimage for SN image (#4365)

* issues for install license file on accton switches (#4460)

* Add test cases for rflash regular usage against openbmc

* modify depending on xuwei's comment

* add 2 more cases for option d

* enhance rflash upload message

* Do not display message for clearing dumps when only PNOR

* Display hostname even if multiple IP addresses

* modified depending on comments

* polished message

* Modify the default consoleondemand based on the global setting

This commit fix the bug that consoleondemand works incorrectly.

* modify depending on comments

* modified depending on comments

* enhance rflash error messages

* Modify documenation for servicenode attributes

* build rst file from Schema.pm by db2man

* change status back to starts

* modified depending on comments

* rm openbmcevents

* Usage and man page update for rspconfig dump

* let rflash error message flexible

* Add support for the "file -> (noderange) file" syntax in synclist with ServiceNodes (#4445)

* Add support for "file -  (noderange) file" in synclist when using
hierarchical mode. Fixes #4425

  This patch ensures that:
  1. the synclist is correctly parsed when running on a Service Node
  2. all files are synchronized to SNs in hierarchical mode

* Better test condition for #4425, addresses issue in
https://github.com/xcat2/xcat-core/pull/4445#issuecomment-349472901

* Fix issue 4477, if has node-<mac> will not create node-<mtms> for the same node

* fix issue updatenode -f loses directories when copying files to SN #4456 (#4494)

* comment from ErTao

* Crude attempt at including external configuration files in named.conf

* Fixes after the review

* Fix issue 4490, record any error when rflash active process

* add -d usage and manpage

* updatenode -F not work in hierachy env as the user name is FQDN of MN (#4484)

* updatenode -F not work in hierachy env (#4455)
 - add trace when -V is enabled
 - get the DSH_FROM_USERID from updatenode client

*  - when 'updatenode -F' need to push SN first, using root as non-root does not have permission write to 'SNsyncfiledir'
 - move the set DSH_FROM_USERID code out of the loop, and also cover remote client case.

* fix issue for command rspconfig hostname=*

* enhance rflash

* Adjust the server used for kernel/initrd and imgurl for petitboot (#4416)
 - URL for kernel/initrd, get the value from below value tftpserver -> xcatmaster -> myipfn
 - URL for image, get the value from below value nfsserver -> tftpserver -> xcatmaster -> myipfn

* NODE attribute didn't populate in /opt/xcat/xcatinfo after reboot (#4428)

* NODE attribute didn't populate in /opt/xcat/xcatinfo after reboot

* Get NODE from mypostscripts

* Improve the output message for reventlog, use a global variable to set PolicyFile Path

* If debug_msg is not provided, use an empty string

* Check for LinkLocal as well as 169.254 IP address

* Fix issue 4507, add parameter check for rspconfig admin_passwd

* record more information when rflash upload error

* Fix the error when using array ref in updatenode with old version perl, it is introduced by PR#4484 (#4518)

* Do not restart conserver if goconserver was started

If goconserver was enabled, do not start conserver when restart
xcatd on service node.

* remove the /etc/localtime before copy timezone file

* Use CONGO_CLIENT_TYPE to tell goconserver the source of client (#4501)

goconserver could send back message based on the client type
this commit set CONGO_CLIENT_TYPE to xcat to make the message
from goconserver more friendly.

* add rflash -d doc

* only ignore 169.254.x.x for OpenBMC

* Fix issue 4513, print out better error msg for reventlog -s

* Modify or add openbmc test cases or bundle

* add test cases for updatenode -f/F in hierarchy environment, covers issues #4456,#4455 and PR #4425 (#4500)
2017-12-14 05:03:34 -06:00

1264 lines
38 KiB
Perl

# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#-------------------------------------------------------
=head1
xCAT plugin package to handle pdu
Supported command:
rpower
rinv
rvitals
=cut
#-------------------------------------------------------
package xCAT_plugin::pdu;
BEGIN {
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr';
}
use lib "$::XCATROOT/lib/perl";
use xCAT::Table;
use xCAT::Utils;
use xCAT::FifoPipe;
use xCAT::MsgUtils;
use xCAT::State;
use xCAT::SvrUtils;
use xCAT::Usage;
use xCAT::NodeRange;
use Data::Dumper;
use Getopt::Long;
use File::Path;
use Term::ANSIColor;
use Time::Local;
use strict;
use Class::Struct;
use XML::Simple;
use Storable qw(dclone);
use SNMP;
use Expect;
use Net::Ping;
my $VERBOSE = 0;
my %allerrornodes = ();
my $callback;
my $pdutab;
my @pduents;
my $pdunodes;
#-------------------------------------------------------
=head3 handled_commands
Return list of commands handled by this plugin
=cut
#-------------------------------------------------------
sub handled_commands
{
return {
rpower => ["nodehm:mgt","pduoutlet:pdu=\.\*"],
rinv => ["nodehm:mgt"],
rvitals => ["nodehm:mgt"],
nodeset => ["nodehm:mgt"],
rspconfig => ["nodehm:mgt"],
pdudiscover => "pdu",
};
}
sub pdu_usage
{
my ($callback, $command) = @_;
my $usagemsg =
"Usage:
pdudiscover [<noderange>|--range ipranges] [-r|-x|-z] [-w] [-V|--verbose] [--setup]
rpower pdunodes [off|on|stat|reset]
The following command supports IR PDU with pdutype=irpdu :
rpower CN [pduoff|pduon|pdustat|pdustatus|pdureset]
The following commands support CR PDU with pdutype=crpdu :
rpower pdunodes relay=[1|2|3] [on|off]
rinv pdunodes
rvitals pdunodes
rspconfig pdunodes sshcfg
rspconfig pdunodes snmpcfg
rspconfig pdunode [hostname=<NAME>|ip=<IP>|mask=<MASK>]
\n";
if ($callback)
{
my $rsp = {};
$rsp->{data}->[0] = $usagemsg;
xCAT::MsgUtils->message("I", $rsp, $callback);
}
else
{
xCAT::MsgUtils->message("I", $usagemsg);
}
return;
}
#--------------------------------------------------------------------------------
=head3 preprocess_request
Parse the arguments and display the usage or the version string.
=cut
#--------------------------------------------------------------------------------
sub preprocess_request {
my $req = shift;
if ($req->{_xcatpreprocessed}->[0] == 1) { return [$req]; }
my $callback=shift;
my @requests;
my $command = $req->{command}->[0];
my $noderange = $req->{node}; #Should be arrayref
my $extrargs = $req->{arg};
my @exargs=($req->{arg});
if (ref($extrargs)) {
@exargs=@$extrargs;
}
my $usage_string=xCAT::Usage->parseCommand($command, @exargs);
if ($usage_string) {
&pdu_usage($callback, $command);
return 1;
}
if ((!$noderange) && ($command ne "pdudiscover") ){
&pdu_usage($callback, $command);
return;
}
my @result = ();
my $mncopy = {%$req};
push @result, $mncopy;
return \@result;
}
#-------------------------------------------------------
=head3 process_request
Process the command.
=cut
#-------------------------------------------------------
sub process_request
{
my $request = shift;
my $callback = shift;
my $subreq = shift;
my $command = $request->{command}->[0];
my $noderange = $request->{node}; #Should be arrayref
my $extrargs = $request->{arg};
my @exargs = ($request->{arg});
if (ref($extrargs)) {
@exargs = @$extrargs;
}
if( $command eq "rinv") {
#for higher performance, handle node in batch
return showMFR($noderange, $callback);
}elsif ($command eq "rvitals") {
return showMonitorData($noderange, $callback);
}elsif ($command eq "rpower") {
my $subcmd = $exargs[0];
my $subcmd2 = $exargs[1];
if (($subcmd eq 'pduoff') || ($subcmd eq 'pduon') || ($subcmd eq 'pdustat')|| ($subcmd eq 'pdureset') ){
#if one day, pdu node have pdu attribute, handle in this section too
return powerpduoutlet($noderange, $subcmd, $callback);
} else {
#-------------------------------------------
#there are 2 cases will enter this block
#one is if node's mgt is pdu
#another is if node has pdu attribute but mgt isn't pdu
#if the node has pdu attribute but mgt isn't pdu,
#should do nothing for this node, let other plugin to hanle this node
#-------------------------------------------
my @allpdunodes=();
my $nodehm = xCAT::Table->new('nodehm');
my $nodehmhash = $nodehm->getNodesAttribs($noderange, ['mgt']);
foreach my $node (@$noderange) {
if($nodehmhash->{$node}->[0]->{mgt} eq 'pdu'){
push @allpdunodes, $node;
}
}
if(@allpdunodes) {
if ( ($subcmd =~ /relay/) || ($subcmd2 =~ /relay/) ) {
process_powerrelay($request,$subreq,\@allpdunodes,$callback);
} elsif(($subcmd eq 'on') || ($subcmd eq 'off') || ($subcmd eq 'stat') || ($subcmd eq 'state') || ($subcmd eq 'reset') ){
return powerpdu(\@allpdunodes, $subcmd, $callback);
} else {
my $pdunode = join (",", @allpdunodes);
$callback->({ errorcode => [1],error => "The option $subcmd is not support for pdu node(s) $pdunode."});
&pdu_usage($callback, $command);
}
}
}
}elsif($command eq "rspconfig") {
my $subcmd = $exargs[0];
if ($subcmd eq 'sshcfg') {
process_sshcfg($noderange, $subcmd, $callback);
}elsif ($subcmd eq 'snmpcfg') {
process_snmpcfg($noderange, $subcmd, $callback);
}elsif ($subcmd =~ /ip|netmask|hostname/) {
process_netcfg($request, $subreq, $subcmd, $callback);
} else {
$callback->({ errorcode => [1],error => "The input $command $subcmd is not support for pdu"});
&pdu_usage($callback, $command);
}
}elsif($command eq "pdudiscover") {
process_pdudiscover($request, $subreq, $callback);
}elsif($command eq "nodeset") {
$callback->({ errorcode => [1],error => "The input $command is not support for pdu"});
&pdu_usage($callback, $command);
}else{
#reserve for other new command in future
}
return;
}
#-------------------------------------------------------
=head3 fill_outletcount
Get outlet count for IR PDU.
=cut
#-------------------------------------------------------
sub fill_outletCount {
my $session = shift;
my $pdu = shift;
my $callback = shift;
my $outletoid = ".1.3.6.1.4.1.2.6.223.8.2.1.0";
my $pdutab = xCAT::Table->new('pdu');
my $count = $session->get("$outletoid");
if ($count) {
$pdutab->setNodeAttribs($pdu, {outlet => $count});
}
return $count;
}
#-------------------------------------------------------
=head3 powerpdu
Process power command (stat/off/on) for pdu/pdus
=cut
#-------------------------------------------------------
sub powerpdu {
my $noderange = shift;
my $subcmd = shift;
my $callback = shift;
if (($subcmd eq "stat") || ($subcmd eq "state")){
return powerstat($noderange, $callback);
}
my $pdutab = xCAT::Table->new('pdu');
my $pduhash = $pdutab->getNodesAttribs($noderange, ['pdutype','outlet']);
foreach my $node (@$noderange) {
if ($pduhash->{$node}->[0]->{pdutype} eq 'crpdu') {
process_relay($node,$subcmd,$callback,1,3);
next;
}
my $session = connectTopdu($node,$callback);
if (!$session) {
$callback->({ errorcode => [1],error => "Couldn't connect to $node"});
next;
}
my $count = $pduhash->{$node}->[0]->{outlet};
unless ($count) {
$count = fill_outletCount($session, $node, $callback);
}
my $value;
my $statstr;
if ($subcmd eq "off") {
$value = 0;
$statstr = "off";
} elsif ( $subcmd eq "on") {
$value = 1;
$statstr = "on";
} else {
$value = 2;
$statstr = "reset";
}
for (my $outlet =1; $outlet <= $count; $outlet++)
{
outletpower($session, $outlet, $value);
if ($session->{ErrorStr}) {
$callback->({ errorcode => [1],error => "Failed to get outlet status for $node"});
} else {
my $output = " outlet $outlet is $statstr";
xCAT::SvrUtils::sendmsg($output, $callback, $node, %allerrornodes);
}
}
}
}
#-------------------------------------------------------
=head3 powerpduoutlet
Process power command (pdustat/pduoff/pduon) for compute nodes,
the pdu attribute needs to be set
=cut
#-------------------------------------------------------
sub powerpduoutlet {
my $noderange = shift;
my $subcmd = shift;
my $callback = shift;
my $output;
my $value;
my $statstr;
my $tmpnodestr = join(",", @$noderange);
my $nodetab = xCAT::Table->new('pduoutlet');
my $nodepdu = $nodetab->getNodesAttribs($noderange,['pdu']);
my $pdutab = xCAT::Table->new('pdu');
my $pduhash = $pdutab->getNodesAttribs($noderange, ['pdutype','outlet']);
foreach my $node (@$noderange) {
# the pdu attribute needs to be set
if(! $nodepdu->{$node}->[0]->{pdu}){
$callback->({ error => "$node: without pdu attribute"});
next;
}
my @pdus = split /,/, $nodepdu->{$node}->[0]->{pdu};
foreach my $pdu_outlet (@pdus) {
my ($pdu, $outlet) = split /:/, $pdu_outlet;
if ($pduhash->{$pdu}->[0]->{pdutype} eq 'crpdu') {
$callback->({ error => "$node: This command doesn't supports CONSTELLATION PDU with pdutype=crpdu for $pdu"});
next;
}
my $session = connectTopdu($pdu,$callback);
if (!$session) {
$callback->({ errorcode => [1],error => "$node: Couldn't connect to $pdu"});
next;
}
my $count = $pduhash->{$pdu}->[0]->{outlet};
unless ($count) {
$count = fill_outletCount($session, $pdu, $callback);
}
if ($outlet > $count ) {
$callback->({ error => "$node: $pdu outlet number $outlet is invalid"});
next;
}
my $cmd;
if ($subcmd eq "pdustat") {
$statstr=outletstat($session, $outlet);
} elsif ($subcmd eq "pduoff") {
$value = 0;
$statstr = "off";
outletpower($session, $outlet, $value);
} elsif ($subcmd eq "pduon") {
$value = 1;
$statstr = "on";
outletpower($session, $outlet, $value);
} elsif ($subcmd eq "pdureset") {
$value = 2;
$statstr = "reset";
outletpower($session, $outlet, $value);
} else {
$callback->({ error => "$subcmd is not support"});
}
if ($session->{ErrorStr}) {
$callback->({ errorcode => [1],error => "$node: $pdu outlet $outlet has error = $session->{ErrorStr}"});
} else {
$output = "$pdu outlet $outlet is $statstr";
xCAT::SvrUtils::sendmsg($output, $callback, $node, %allerrornodes);
}
}
}
}
#-------------------------------------------------------
=head3 outletpower
Process power command for one pdu outlet,
=cut
#-------------------------------------------------------
sub outletpower {
my $session = shift;
my $outlet = shift;
my $value = shift;
my $oid = ".1.3.6.1.4.1.2.6.223.8.2.2.1.11";
my $type = "INTEGER";
if ($session->{newmib}) {
$oid = ".1.3.6.1.4.1.2.6.223.8.2.2.1.13";
}
my $varbind = new SNMP::Varbind([ $oid, $outlet, $value, $type ]);
return $session->set($varbind);
}
#-------------------------------------------------------
=head3 powerstat
Process command to query status of pdu
=cut
#-------------------------------------------------------
sub powerstat {
my $noderange = shift;
my $callback = shift;
my $output;
my $pdutab = xCAT::Table->new('pdu');
my $pduhash = $pdutab->getNodesAttribs($noderange, ['pdutype','outlet','snmpversion','snmpuser','authtype','authkey','privtype','privkey','seclevel']);
foreach my $pdu (@$noderange) {
if ($pduhash->{$pdu}->[0]->{pdutype} eq 'crpdu') {
my $snmpversion = $pduhash->{$pdu}->[0]->{snmpversion};
if ($snmpversion =~ /3/) {
my $snmpuser = $pduhash->{$pdu}->[0]->{snmpuser};
my $seclevel = $pduhash->{$pdu}->[0]->{seclevel};
if ((defined $snmpuser) && (defined $seclevel)) {
my $authtype = $pduhash->{$pdu}->[0]->{authtype};
if (!defined $authtype) {
$authtype="MD5";
}
my $authkey = $pduhash->{$pdu}->[0]->{authkey};
my $privtype = $pduhash->{$pdu}->[0]->{privtype};
if (!defined $privtype) {
$privtype="DES";
}
my $privkey = $pduhash->{$pdu}->[0]->{privkey};
if (!defined $privkey) {
if (defined $authkey) {
$privkey=$authkey;
}
}
my $snmpcmd;
if ($seclevel eq "authNoPriv") {
$snmpcmd = "snmpwalk -v3 -u $snmpuser -a $authtype -A $authkey -l $seclevel";
} elsif ($seclevel eq "authPriv") {
$snmpcmd = "snmpwalk -v3 -u $snmpuser -a $authtype -A $authkey -l $seclevel -x $privtype -X $privkey";
} else { #default to notAuthNoPriv
$snmpcmd = "snmpwalk -v3 -u $snmpuser -l $seclevel";
}
for (my $relay = 1; $relay <= 3; $relay++) {
relaystat($pdu, $relay, $snmpcmd, $callback);
}
next;
}
}
xCAT::SvrUtils::sendmsg("please config snmpv3 to be able to query pdu relay status", $callback,$pdu);
xCAT::SvrUtils::sendmsg(" use chdef command to add pdu snmpv3 attributes to pdu table", $callback);
xCAT::SvrUtils::sendmsg(" then run 'rspconfig $pdu snmpcfg' command ", $callback);
next;
}
my $session = connectTopdu($pdu,$callback);
if (!$session) {
$callback->({ errorcode => [1],error => "Couldn't connect to $pdu"});
next;
}
my $count = $pduhash->{$pdu}->[0]->{outlet};
unless ($count) {
$count = fill_outletCount($session, $pdu, $callback);
}
for (my $outlet =1; $outlet <= $count; $outlet++)
{
my $statstr = outletstat($session, $outlet);
my $msg = " outlet $outlet is $statstr";
xCAT::SvrUtils::sendmsg($msg, $callback, $pdu, %allerrornodes);
}
}
}
#-------------------------------------------------------
=head3 outletstat
Process command to query status of one pdu outlet
=cut
#-------------------------------------------------------
sub outletstat {
my $session = shift;
my $outlet = shift;
my $oid = ".1.3.6.1.4.1.2.6.223.8.2.2.1.11";
my $output;
my $statstr;
if ($session->{newmib}) {
$oid = ".1.3.6.1.4.1.2.6.223.8.2.2.1.13";
}
$output = $session->get("$oid.$outlet");
if ($output eq 1) {
$statstr = "on";
} elsif ($output eq 0) {
$statstr = "off";
} else {
return;
}
return $statstr;
}
#-------------------------------------------------------
=head3 connectTopdu
connect pdu via snmp session
=cut
#-------------------------------------------------------
sub connectTopdu {
my $pdu = shift;
my $callback = shift;
my $snmpver = "1";
my $community = "public";
my $session;
my $msg = "connectTopdu";
my $versionoid = ".1.3.6.1.4.1.2.6.223.7.3.0";
$session = new SNMP::Session(
DestHost => $pdu,
Version => $snmpver,
Community => $community,
UseSprintValue => 1,
);
unless ($session) {
return;
}
my $varbind = new SNMP::Varbind([ $versionoid, '' ]);
my $pduversion = $session->get($varbind);
if ($session->{ErrorStr}) {
return;
}
$session->{newmib} = 0;
if ($pduversion =~ /sLEN/) {
$session->{newmib} = 1;
}
return $session;
}
#-------------------------------------------------------
=head3 process_netcfg
Config hostname of PDU
Config ip/netmask of PDU via PduManager command
PduManager is a tool for CoralPdu to manager the PDU.
* /dev/shm/bin/PduManager -h
'-i' set PDU system IP
'-n' set system ip netmask. e.g.:PduManager -i xxx.xxx.xxx.xxx -n xxx.xxx.xxx.xxx
example: rspconfig coralpdu hostname=coralpdu
rspconfig coralpdu ip=1.1.1.1 netmask=255.0.0.0
=cut
#-------------------------------------------------------
sub process_netcfg {
my $request = shift;
my $subreq = shift;
my $subcmd = shift;
my $callback = shift;
my $hostname;
my $ip;
my $netmask;
my $args;
my $exp;
my $errstr;
my $extrargs = $request->{arg};
my @exargs = ($request->{arg});
if (ref($extrargs)) {
@exargs = @$extrargs;
}
my $nodes = $request->{node};
my $node_number = @$nodes;
if ($node_number gt "1") {
xCAT::SvrUtils::sendmsg("Can not configure more than 1 nodes", $callback);
return;
}
my $pdu = @$nodes[0];
my $rsp = {};
my $pdutab = xCAT::Table->new('pdu');
my $pduhash = $pdutab->getNodesAttribs($nodes, ['pdutype']);
unless ($pduhash->{$pdu}->[0]->{pdutype} eq "crpdu") {
xCAT::SvrUtils::sendmsg("This command only supports CONSTELLATION PDU with pdutype=crpdu", $callback,$pdu);
return;
}
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs($nodes,['ip','otherinterfaces']);
# connect to PDU
my $static_ip = $nodehash->{$pdu}->[0]->{ip};
my $discover_ip = $nodehash->{$pdu}->[0]->{otherinterfaces};
($exp, $errstr) = session_connect($static_ip, $discover_ip);
if (defined $errstr) {
xCAT::SvrUtils::sendmsg("Failed to connect", $callback,$pdu);
return;
}
foreach my $cmd (@exargs) {
my ($key, $value) = split(/=/, $cmd);
if ($key =~ /hostname/) {
$hostname = $value;
my ($ret, $err) = session_exec($exp, "echo $hostname > /etc/hostname;/etc/init.d/hostname.sh");
if (defined $err) {
xCAT::SvrUtils::sendmsg("Failed to set hostname", $callback);
}
}elsif ($key =~ /ip/) {
$ip = $value;
} elsif ($key =~ /netmask/) {
$netmask = $value;
} else {
xCAT::SvrUtils::sendmsg("rspconfig $cmd is not support yet, ignored", $callback);
}
}
$args = "/dev/shm/bin/PduManager ";
my $opt;
if ($ip) {
$opt = "-i $ip ";
}
if ($netmask) {
$opt = $opt . "-n $netmask";
}
if ($opt) {
my $dshcmd = $args . $opt ;
my ($ret, $err) = session_exec($exp, $dshcmd);
if (defined $err) {
#session will be hung if ip address changed
my $p = Net::Ping->new();
if ( ($p->ping($ip)) && ($err =~ /TIMEOUT/) ) {
xCAT::SvrUtils::sendmsg("$ip is reachable", $callback);
} else {
xCAT::SvrUtils::sendmsg("Failed to run $dshcmd, error=$err", $callback);
return;
}
}
xCAT::SvrUtils::sendmsg("$dshcmd ran successfully", $callback);
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$pdu,"ip=$ip","otherinterfaces="] }, $subreq, 0, 1);
xCAT::Utils->runxcmd({ command => ['makehosts'], node => [$pdu] }, $subreq, 0, 1);
}
if (defined $exp) {
$exp->hard_close();
}
}
#-------------------------------------------------------
=head3 process_sshcfg
Config passwordless for coralpdu
example: rspconfig coralpdu sshcfg
=cut
#-------------------------------------------------------
sub process_sshcfg {
my $noderange = shift;
my $subcmd = shift;
my $callback = shift;
#this is default password for CoralPDU
my $password = "password8";
my $userid = "root";
my $timeout = 30;
my $keyfile = "/root/.ssh/id_rsa.pub";
my $rootkey = `cat /root/.ssh/id_rsa.pub`;
my $cmd;
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs($noderange,['ip','otherinterfaces']);
my $pdutab = xCAT::Table->new('pdu');
my $pduhash = $pdutab->getNodesAttribs($noderange, ['pdutype']);
foreach my $pdu (@$noderange) {
unless ($pduhash->{$pdu}->[0]->{pdutype} eq "crpdu") {
xCAT::SvrUtils::sendmsg("This command only supports CONSTELLATION PDU with pdutype=crpdu", $callback,$pdu);
next;
}
my $msg = " process_sshcfg";
xCAT::SvrUtils::sendmsg($msg, $callback, $pdu, %allerrornodes);
#remove old host key from /root/.ssh/known_hosts
$cmd = "ssh-keygen -R $pdu";
xCAT::Utils->runcmd($cmd, 0);
my $static_ip = $nodehash->{$pdu}->[0]->{ip};
my $discover_ip = $nodehash->{$pdu}->[0]->{otherinterfaces};
my ($exp, $errstr) = session_connect($static_ip, $discover_ip);
if (!defined $exp) {
$msg = " Failed to connect $errstr";
xCAT::SvrUtils::sendmsg($msg, $callback, $pdu, %allerrornodes);
next;
}
my $ret;
my $err;
($ret, $err) = session_exec($exp, "mkdir -p /home/root/.ssh");
($ret, $err) = session_exec($exp, "chmod 700 /home/root/.ssh");
($ret, $err) = session_exec($exp, "echo \"$rootkey\" >/home/root/.ssh/authorized_keys");
($ret, $err) = session_exec($exp, "chmod 644 /home/root/.ssh/authorized_keys");
$exp->hard_close();
}
return;
}
#-------------------------------------------------------
=head3 session_connect
open a expect session and connect to CR PDU.
=cut
#-------------------------------------------------------
sub session_connect {
my $static_ip = shift;
my $discover_ip = shift;
#default password for coral pdu
my $password = "password8";
my $userid = "root";
my $timeout = 30;
my $ssh_ip;
my $p = Net::Ping->new();
if ($p->ping($static_ip)) {
$ssh_ip = $static_ip;
} elsif ($p->ping($discover_ip)) {
$ssh_ip = $discover_ip;
} else {
return(undef, " is not reachable\n");
}
my $ssh = Expect->new;
my $command = 'ssh';
my @parameters = ($userid . "@" . $ssh_ip);
$ssh->debug(0);
$ssh->log_stdout(0); # suppress stdout output..
$ssh->slave->stty(qw(sane -echo));
unless ($ssh->spawn($command, @parameters))
{
my $err = $!;
$ssh->soft_close();
my $rsp;
return(undef, "unable to run command $command $err\n");
}
$ssh->expect($timeout,
[ "-re", qr/WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED/, sub {die "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!\n"; } ],
[ "-re", qr/\(yes\/no\)\?\s*$/, sub { $ssh->send("yes\n"); exp_continue; } ],
[ "-re", qr/ password:/, sub {$ssh->send("$password\n"); exp_continue; } ],
[ "-re", qr/:~\$/, sub { $ssh->send("sudo su\n"); exp_continue; } ],
[ "-re", qr/.*#/, sub { $ssh->clear_accum(); } ],
[ timeout => sub { die "No login.\n"; } ]
);
$ssh->clear_accum();
return ($ssh);
}
#-------------------------------------------------------
=head3 session_exec
execute command to CR PDU.
=cut
#-------------------------------------------------------
sub session_exec {
my $exp = shift;
my $cmd = shift;
my $timeout = shift;
my $prompt = shift;
$timeout = 30 unless defined $timeout;
$prompt = qr/.*#/ unless defined $prompt;
$exp->clear_accum();
$exp->send("$cmd\n");
my ($mpos, $merr, $mstr, $mbmatch, $mamatch) = $exp->expect(6, "-re", $prompt);
if (defined $merr) {
return(undef,$merr);
}
return($mbmatch);
}
#-----------------------------------------------------------------
=head3 process_pdudiscover
Discover the pdu for a given range of DHCP ip address
it will call switchdiscover command with -s snmp --pdu options
example: pdudiscover --range iprange -w
=cut
#------------------------------------------------------------------
sub process_pdudiscover {
my $request = shift;
my $sub_req = shift;
my $callback = shift;
my $extrargs = $request->{arg};
my @exargs = ($request->{arg});
if (ref($extrargs)) {
@exargs=@$extrargs;
}
#check case in GetOptions
$Getopt::Long::ignorecase = 0;
Getopt::Long::Configure( "bundling" );
Getopt::Long::Configure("no_pass_through");
my %opt;
if (!GetOptions( \%opt,
qw(h|help V|verbose x z w r range=s setup))) {
my $usage_string = xCAT::Usage->getUsage($request->{command}->[0]);
$callback->({ data => $usage_string });
return;
}
push @exargs, "-s snmp --pdu";
my $cmd = "switchdiscover @exargs";
my $result = xCAT::Utils->runcmd($cmd, 0);
my $rsp = {};
push @{ $rsp->{data} }, "$result";
xCAT::MsgUtils->message("I", $rsp, $callback);
}
#-------------------------------------------------------
=head3 showMFR
show MFR information of PDU via PduManager command
PduManager is a tool for CoralPdu to manager the PDU.
* /dev/shm/bin/PduManager -h
'-m' show MFR info
example: rinv coralpdu
=cut
#-------------------------------------------------------
sub showMFR {
my $noderange = shift;
my $callback = shift;
my $output;
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs($noderange,['ip','otherinterfaces']);
my $pdutab = xCAT::Table->new('pdu');
my $pduhash = $pdutab->getNodesAttribs($noderange, ['pdutype']);
foreach my $pdu (@$noderange) {
unless ($pduhash->{$pdu}->[0]->{pdutype} eq "crpdu") {
xCAT::SvrUtils::sendmsg("This command only supports CONSTELLATION PDU with pdutype=crpdu", $callback,$pdu);
next;
}
# connect to PDU
my $static_ip = $nodehash->{$pdu}->[0]->{ip};
my $discover_ip = $nodehash->{$pdu}->[0]->{otherinterfaces};
my ($exp, $errstr) = session_connect($static_ip, $discover_ip);
if (defined $errstr) {
xCAT::SvrUtils::sendmsg("Failed to connect: $errstr", $callback);
}
my ($ret, $err) = session_exec($exp, "/dev/shm/bin/PduManager -m");
if (defined $err) {
xCAT::SvrUtils::sendmsg("Failed to list MFR information: $err", $callback);
}
if (defined $ret) {
foreach my $line (split /[\r\n]+/, $ret) {
if ($line) {
$line = join(' ',split(' ',$line));
xCAT::SvrUtils::sendmsg("$line", $callback,$pdu);
}
}
}
$exp->hard_close();
}
}
#-------------------------------------------------------
=head3 showMonitorData
Show realtime monitor data(input voltage, current, power)
of PDU via PduManager command
PduManager is a tool for CoralPdu to manager the PDU.
* /dev/shm/bin/PduManager -h
'-d' show realtime monitor data(input voltage, current, power)
example: rvitals coralpdu
=cut
#-------------------------------------------------------
sub showMonitorData {
my $noderange = shift;
my $callback = shift;
my $output;
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs($noderange,['ip','otherinterfaces']);
my $pdutab = xCAT::Table->new('pdu');
my $pduhash = $pdutab->getNodesAttribs($noderange, ['pdutype']);
foreach my $pdu (@$noderange) {
unless ($pduhash->{$pdu}->[0]->{pdutype} eq "crpdu") {
xCAT::SvrUtils::sendmsg("This command only supports CONSTELLATION PDU with pdutype=crpdu", $callback,$pdu);
next;
}
# connect to PDU
my $static_ip = $nodehash->{$pdu}->[0]->{ip};
my $discover_ip = $nodehash->{$pdu}->[0]->{otherinterfaces};
my ($exp, $errstr) = session_connect($static_ip, $discover_ip);
my $ret;
my $err;
($ret, $err) = session_exec($exp, "/dev/shm/bin/PduManager -d");
if (defined $err) {
xCAT::SvrUtils::sendmsg("Failed to show monitor data: $err", $callback);
}
if (defined $ret) {
foreach my $line (split /[\r\n]+/, $ret) {
if ($line) {
$line = join(' ',split(' ',$line));
xCAT::SvrUtils::sendmsg("$line", $callback,$pdu);
}
}
}
$exp->hard_close();
}
}
#-------------------------------------------------------
=head3 relaystat
process individual relay stat for CR PDU.
The OID for 3 relay:
1.3.6.1.4.1.2.6.262.15.2.13
1.3.6.1.4.1.2.6.262.15.2.14
1.3.6.1.4.1.2.6.262.15.2.15
=cut
#-------------------------------------------------------
sub relaystat {
my $pdu = shift;
my $relay = shift;
my $snmpcmd = shift;
my $callback = shift;
my $relayoid = $relay + 12;
#default pdu snmpv3, won't show up for snmpv1
my $cmd = "$snmpcmd $pdu 1.3.6.1.4.1.2.6.262.15.2.$relayoid";
my $result = xCAT::Utils->runcmd($cmd, 0);
my ($msg,$stat) = split /: /, $result;
if ($stat eq "1" ) {
xCAT::SvrUtils::sendmsg(" relay $relay is on", $callback, $pdu, %allerrornodes);
} elsif ( $stat eq "0" ) {
xCAT::SvrUtils::sendmsg(" relay $relay is off", $callback, $pdu, %allerrornodes);
} else {
xCAT::SvrUtils::sendmsg(" relay $relay is unknown", $callback, $pdu, %allerrornodes);
}
return;
}
#-------------------------------------------------------
=head3 process_powerrelay
process relay action for CR PDU.
=cut
#-------------------------------------------------------
sub process_powerrelay {
my $request = shift;
my $subreq = shift;
my $subcmd = shift;
my $callback = shift;
my $relay;
my $action;
my $extrargs = $request->{arg};
my @exargs = ($request->{arg});
if (ref($extrargs)) {
@exargs = @$extrargs;
}
my $nodes = $request->{node};
foreach my $cmd (@exargs) {
if ($cmd =~ /=/ ) {
my ($key, $value) = split(/=/, $cmd);
$relay = $value;
} else {
$action = $cmd;
}
}
if ( (defined $relay) && (defined $action) ) {
my $relay_count = 1;
foreach my $pdu (@$nodes) {
process_relay($pdu, $action, $callback, $relay, $relay_count);
}
} else {
xCAT::SvrUtils::sendmsg(" This command is not support, please define relay number and action", $callback);
}
}
#-------------------------------------------------------
=head3 process_relay
process relay action for CR PDU.
=cut
#-------------------------------------------------------
sub process_relay {
my $pdu = shift;
my $subcmd = shift;
my $callback = shift;
my $relay_num = shift;
my $relay_count = shift;
if ( !defined $relay_count ) {
$relay_num = 1;
$relay_count = 3;
}
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodeAttribs($pdu,['ip','otherinterfaces']);
# connect to PDU
my $static_ip = $nodehash->{$pdu}->[0]->{ip};
my $discover_ip = $nodehash->{$pdu}->[0]->{otherinterfaces};
my ($session, $errstr) = session_connect($static_ip, $discover_ip);
my $ret;
my $err;
my $statestr;
for (my $i = 0; $i < $relay_count; $i++) {
my $relay = $relay_num;
xCAT::SvrUtils::sendmsg(" power $subcmd for relay $relay_num", $callback,$pdu);
if ($subcmd eq "off") {
relay_action($session, $pdu, $relay, "OFF", $callback);
} elsif ( $subcmd eq "on") {
relay_action($session, $pdu, $relay, "ON", $callback);
} elsif ( $subcmd eq "reset") {
relay_action($session, $pdu, $relay, "OFF", $callback);
relay_action($session, $pdu, $relay, "ON", $callback);
} else {
xCAT::SvrUtils::sendmsg(" subcmd $subcmd is not support", $callback,$pdu);
}
$relay_num++;
}
$session->hard_close();
}
#-------------------------------------------------------
=head3 realy_action
process individual relay action for CR PDU.
=cut
#-------------------------------------------------------
sub relay_action {
my $session = shift;
my $pdu = shift;
my $relay = shift;
my $action = shift;
my $callback = shift;
my ($ret, $err) = session_exec($session, "/dev/shm/bin/PduManager -r $relay -v $action");
if (defined $err) {
xCAT::SvrUtils::sendmsg("Failed to process relay action: $err", $callback);
}
if (defined $ret) {
xCAT::SvrUtils::sendmsg("$ret", $callback,$pdu);
}
}
#-------------------------------------------------------
=head3 process_snmpcfg
config snmp and snmpv3 for CR PDU.
=cut
#-------------------------------------------------------
sub process_snmpcfg {
my $noderange = shift;
my $subcmd = shift;
my $callback = shift;
my $snmp_conf="/etc/snmp/snmpd.conf";
my $xCATSettingsSTART="xCAT settings START";
my $xCATSettingsEND="xCAT settings END";
my $xCATSettingsInfo="Entries between the START and END lines will be replaced each time by xCAT command";
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs($noderange,['ip','otherinterfaces']);
my $pdutab = xCAT::Table->new('pdu');
my $pduhash = $pdutab->getNodesAttribs($noderange, ['pdutype','community','snmpversion','snmpuser','authtype','authkey','privtype','privkey','seclevel']);
foreach my $pdu (@$noderange) {
unless ($pduhash->{$pdu}->[0]->{pdutype} eq "crpdu") {
xCAT::SvrUtils::sendmsg("This command only supports CONSTELLATION PDU with pdutype=crpdu", $callback,$pdu);
next;
}
my $community = $pduhash->{$pdu}->[0]->{community};
my $snmpversion = $pduhash->{$pdu}->[0]->{snmpversion};
my $snmpuser = $pduhash->{$pdu}->[0]->{snmpuser};
my $authtype = $pduhash->{$pdu}->[0]->{authtype};
if (!defined $authtype) {
$authtype="MD5";
}
my $authkey = $pduhash->{$pdu}->[0]->{authkey};
my $privtype = $pduhash->{$pdu}->[0]->{privtype};
if (!defined $privtype) {
$privtype="DES";
}
my $privkey = $pduhash->{$pdu}->[0]->{privkey};
if (!defined $privkey) {
if (defined $authkey) {
$privkey=$authkey;
}
}
my $seclevel = $pduhash->{$pdu}->[0]->{seclevel};
# connect to PDU
my $static_ip = $nodehash->{$pdu}->[0]->{ip};
my $discover_ip = $nodehash->{$pdu}->[0]->{otherinterfaces};
my ($exp, $errstr) = session_connect($static_ip, $discover_ip);
my $ret;
my $err;
($ret, $err) = session_exec($exp, "sed -i '/$xCATSettingsSTART/,/$xCATSettingsEND/ d' $snmp_conf");
($ret, $err) = session_exec($exp, "echo '# $xCATSettingsSTART' >> $snmp_conf");
($ret, $err) = session_exec($exp, "echo '# $xCATSettingsInfo' >> $snmp_conf");
if (defined $community) {
($ret, $err) = session_exec($exp, "echo 'com2sec readwrite default $community' >> $snmp_conf");
}
#set snmpv3 configuration
if ($snmpversion =~ /3/) {
if ((defined $snmpuser) && (defined $seclevel)) {
my $msg1;
if ($seclevel eq "authNoPriv") {
$msg1 = "createUser $snmpuser $authtype $authkey";
} elsif ($seclevel eq "authPriv") {
$msg1 = "createUser $snmpuser $authtype $authkey $privtype $privkey";
} else { #default to notAuthNoPriv
$msg1 = "createUser $snmpuser";
}
my $msg2 = "rwuser $snmpuser $seclevel .1.3.6.1.4.1.2.6.262";
($ret, $err) = session_exec($exp, "sed -i '/\"$snmpuser\"/ d' /var/lib/net-snmp/snmpd.conf");
($ret, $err) = session_exec($exp, "echo $msg1 >> $snmp_conf");
($ret, $err) = session_exec($exp, "echo $msg2 >> $snmp_conf");
} else {
xCAT::SvrUtils::sendmsg("Need to define user name and security level for snmpv3 configuration", $callback);
}
}
($ret, $err) = session_exec($exp, "echo '# $xCATSettingsEND' >> $snmp_conf");
#need to restart snmpd after config file changes
($ret, $err) = session_exec($exp, "ps | grep snmpd | grep -v grep | awk '{ print $1}' | xargs kill -9");
($ret, $err) = session_exec($exp, "/usr/sbin/snmpd -Lsd -Lf /dev/null -p /var/run/snmpd");
if (defined $err) {
xCAT::SvrUtils::sendmsg("Failed to configure snmp : $err", $callback);
}
$exp->hard_close();
}
}
1;