/opsview/files/default/server_plugins/check_vmware_api.pl

https://github.com/masamiya/chef-cookbooks · Perl · 4718 lines · 4337 code · 323 blank · 58 comment · 684 complexity · 35bada396d7dcd8e110ba0ede39fdb40 MD5 · raw file

  1. #!/usr/bin/perl -w
  2. #
  3. # Nagios plugin to monitor VMware ESX and vSphere servers
  4. #
  5. # License: GPL
  6. # Copyright (c) 2008-2013 op5 AB
  7. # Author: Kostyantyn Hushchyn and op5 <op5-users@lists.op5.com>
  8. #
  9. # Contributors:
  10. #
  11. # Patrick M端ller, Jeremy Martin, Eric Jonsson, stumpr,
  12. # John Cavanaugh, Libor Klepac, maikmayers, Steffen Poulsen,
  13. # Mark Elliott, simeg, sebastien.prudhomme, Raphael Schitz,
  14. # Mattias Bergsten
  15. #
  16. # For direct contact with any of the op5 developers, send an email to
  17. # op5-users@lists.op5.com
  18. #
  19. # Discussions are directed to the mailing list op5-users@lists.op5.com,
  20. # see http://lists.op5.com/mailman/listinfo/op5-users
  21. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License version 2 as
  24. # published by the Free Software Foundation.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. #
  31. # You should have received a copy of the GNU General Public License
  32. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. #
  34. # Prevent SSL certificate validation
  35. $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
  36. package CheckVMwareAPI;
  37. use strict;
  38. use warnings;
  39. use vars qw($PROGNAME $VERSION $output $values $result $defperfargs);
  40. use Nagios::Plugin::Functions qw(%STATUS_TEXT);
  41. use Nagios::Plugin;
  42. use File::Basename;
  43. use HTTP::Date;
  44. use Data::Dumper qw(Dumper);
  45. my $perl_module_instructions="
  46. Download the latest version of the vSphere SDK for Perl from VMware.
  47. In this example we use VMware-vSphere-Perl-SDK-5.1.0-780721.x86_64.tar.gz,
  48. but the instructions should apply to other versions as well.
  49. You may need to install additional packages and Perl modules on your server,
  50. see http://www.op5.com/how-to/how-to-install-vmware-vsphere-sdk-perl-5-1/ for
  51. more information and package names for op5 APS / CentOS 6 / RHEL 6.
  52. Upload the .tar.gz file to your op5 Monitor server's /root dir and execute:
  53. cd /root
  54. tar xvzf VMware-vSphere-Perl-SDK-5.1.0-780721.x86_64.tar.gz
  55. cd vmware-vsphere-cli-distrib/
  56. ./vmware-install.pl
  57. Follow the on screen instructions, described below:
  58. \"Creating a new vSphere CLI installer database using the tar4 format.
  59. Installing vSphere CLI 5.1.0 build-780721 for Linux.
  60. You must read and accept the vSphere CLI End User License Agreement to
  61. continue.
  62. Press enter to display it.\"
  63. <ENTER>
  64. \"Read through the License Agreement\"
  65. \"Do you accept? (yes/no)
  66. yes
  67. \"In which directory do you want to install the executable files? [/usr/bin]\"
  68. <ENTER>
  69. \"Please wait while copying vSphere CLI files...
  70. The installation of vSphere CLI 5.1.0 build-780721 for Linux completed
  71. successfully. You can decide to remove this software from your system at any
  72. time by invoking the following command:
  73. \"/usr/bin/vmware-uninstall-vSphere-CLI.pl\".
  74. This installer has successfully installed both vSphere CLI and the vSphere SDK
  75. for Perl.
  76. The following Perl modules were found on the system but may be too old to work
  77. with vSphere CLI:
  78. Compress::Zlib 2.037 or newer
  79. Compress::Raw::Zlib 2.037 or newer
  80. version 0.78 or newer
  81. IO::Compress::Base 2.037 or newer
  82. IO::Compress::Zlib::Constants 2.037 or newer
  83. LWP::Protocol::https 5.805 or newer
  84. Enjoy,
  85. --the VMware team\"
  86. Note: None of the Perl modules mentioned as \"may be too old\" are needed for check_vmware_api to work.
  87. ";
  88. sub main {
  89. $PROGNAME = basename($0);
  90. $VERSION = '0.7.1';
  91. my $np = Nagios::Plugin->new(
  92. usage => "Usage: %s -D <data_center> | -H <host_name> [ -C <cluster_name> ] [ -N <vm_name> ]\n"
  93. . " -u <user> -p <pass> | -f <authfile>\n"
  94. . " -l <command> [ -s <subcommand> ] [ -T <timeshift> ] [ -i <interval> ]\n"
  95. . " [ -x <black_list> ] [ -o <additional_options> ]\n"
  96. . " [ -t <timeout> ] [ -w <warn_range> ] [ -c <crit_range> ]\n"
  97. . ' [ -V ] [ -h ]',
  98. version => $VERSION,
  99. plugin => $PROGNAME,
  100. shortname => uc($PROGNAME),
  101. blurb => 'VMware ESX/vSphere plugin',
  102. extra => "Supported commands(^ - blank or not specified parameter, o - options, T - timeshift value, b - blacklist) :\n"
  103. . " VM specific :\n"
  104. . " * cpu - shows cpu info\n"
  105. . " + usage - CPU usage in percentage\n"
  106. . " + usagemhz - CPU usage in MHz\n"
  107. . " + wait - CPU wait time in ms\n"
  108. . " + ready - CPU ready time in ms\n"
  109. . " ^ all cpu info(no thresholds)\n"
  110. . " * mem - shows mem info\n"
  111. . " + usage - mem usage in percentage\n"
  112. . " + usagemb - mem usage in MB\n"
  113. . " + swap - swap mem usage in MB\n"
  114. . " + swapin - swapin mem usage in MB\n"
  115. . " + swapout - swapout mem usage in MB\n"
  116. . " + overhead - additional mem used by VM Server in MB\n"
  117. . " + overall - overall mem used by VM Server in MB\n"
  118. . " + active - active mem usage in MB\n"
  119. . " + memctl - mem used by VM memory control driver(vmmemctl) that controls ballooning\n"
  120. . " ^ all mem info(except overall and no thresholds)\n"
  121. . " * net - shows net info\n"
  122. . " + usage - overall network usage in KBps(Kilobytes per Second)\n"
  123. . " + receive - receive in KBps(Kilobytes per Second)\n"
  124. . " + send - send in KBps(Kilobytes per Second)\n"
  125. . " ^ all net info(except usage and no thresholds)\n"
  126. . " * io - shows disk I/O info\n"
  127. . " + usage - overall disk usage in MB/s\n"
  128. . " + read - read latency in ms (totalReadLatency.average)\n"
  129. . " + write - write latency in ms (totalWriteLatency.average)\n"
  130. . " ^ all disk io info(no thresholds)\n"
  131. . " * runtime - shows runtime info\n"
  132. . " + con - connection state\n"
  133. . " + cpu - allocated CPU in MHz\n"
  134. . " + mem - allocated mem in MB\n"
  135. . " + state - virtual machine state (UP, DOWN, SUSPENDED)\n"
  136. . " + status - overall object status (gray/green/red/yellow)\n"
  137. . " + consoleconnections - console connections to VM\n"
  138. . " + guest - guest OS status, needs VMware Tools\n"
  139. . " + tools - VMWare Tools status\n"
  140. . " + issues - all issues for the host\n"
  141. . " ^ all runtime info(except con and no thresholds)\n"
  142. . " Host specific :\n"
  143. . " * cpu - shows cpu info\n"
  144. . " + usage - CPU usage in percentage\n"
  145. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  146. . " + usagemhz - CPU usage in MHz\n"
  147. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  148. . " ^ all cpu info\n"
  149. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  150. . " * mem - shows mem info\n"
  151. . " + usage - mem usage in percentage\n"
  152. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  153. . " + usagemb - mem usage in MB\n"
  154. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  155. . " + swap - swap mem usage in MB\n"
  156. . " o listvm - turn on/off output list of swapping VM's\n"
  157. . " + overhead - additional mem used by VM Server in MB\n"
  158. . " + overall - overall mem used by VM Server in MB\n"
  159. . " + memctl - mem used by VM memory control driver(vmmemctl) that controls ballooning\n"
  160. . " o listvm - turn on/off output list of ballooning VM's\n"
  161. . " ^ all mem info(except overall and no thresholds)\n"
  162. . " * net - shows net info\n"
  163. . " + usage - overall network usage in KBps(Kilobytes per Second)\n"
  164. . " + receive - receive in KBps(Kilobytes per Second)\n"
  165. . " + send - send in KBps(Kilobytes per Second)\n"
  166. . " + nic - makes sure all active NICs are plugged in\n"
  167. . " ^ all net info(except usage and no thresholds)\n"
  168. . " * io - shows disk io info\n"
  169. . " + aborted - aborted commands count\n"
  170. . " + resets - bus resets count\n"
  171. . " + read - read latency in ms (totalReadLatency.average)\n"
  172. . " + write - write latency in ms (totalWriteLatency.average)\n"
  173. . " + kernel - kernel latency in ms\n"
  174. . " + device - device latency in ms\n"
  175. . " + queue - queue latency in ms\n"
  176. . " ^ all disk io info\n"
  177. . " * vmfs - shows Datastore info\n"
  178. . " + (name) - free space info for datastore with name (name)\n"
  179. . " o used - output used space instead of free\n"
  180. . " o breif - list only alerting volumes\n"
  181. . " o regexp - whether to treat name as regexp\n"
  182. . " o blacklistregexp - whether to treat blacklist as regexp\n"
  183. . " b - blacklist VMFS's\n"
  184. . " T (value) - timeshift to detemine if we need to refresh\n"
  185. . " ^ all datastore info\n"
  186. . " o used - output used space instead of free\n"
  187. . " o breif - list only alerting volumes\n"
  188. . " o blacklistregexp - whether to treat blacklist as regexp\n"
  189. . " b - blacklist VMFS's\n"
  190. . " T (value) - timeshift to detemine if we need to refresh\n"
  191. . " * runtime - shows runtime info\n"
  192. . " + con - connection state\n"
  193. . " + health - checks cpu/storage/memory/sensor status and propagates worst state\n"
  194. . " o listitems - list all available sensors(use for listing purpose only)\n"
  195. . " o blackregexpflag - whether to treat blacklist as regexp\n"
  196. . " b - blacklist status objects\n"
  197. . " + storagehealth - storage status check\n"
  198. . " o blackregexpflag - whether to treat blacklist as regexp\n"
  199. . " b - blacklist status objects\n"
  200. . " + temperature - temperature sensors\n"
  201. . " o blackregexpflag - whether to treat blacklist as regexp\n"
  202. . " b - blacklist status objects\n"
  203. . " + sensor - threshold specified sensor\n"
  204. . " + maintenance - shows whether host is in maintenance mode\n"
  205. . " + list(vm) - list of VMWare machines and their statuses\n"
  206. . " + status - overall object status (gray/green/red/yellow)\n"
  207. . " + issues - all issues for the host\n"
  208. . " b - blacklist issues\n"
  209. . " ^ all runtime info(health, storagehealth, temperature and sensor are represented as one value and no thresholds)\n"
  210. . " * service - shows Host service info\n"
  211. . " + (names) - check the state of one or several services specified by (names), syntax for (names):<service1>,<service2>,...,<serviceN>\n"
  212. . " ^ show all services\n"
  213. . " * storage - shows Host storage info\n"
  214. . " + adapter - list bus adapters\n"
  215. . " b - blacklist adapters\n"
  216. . " + lun - list SCSI logical units\n"
  217. . " b - blacklist LUN's\n"
  218. . " + path - list logical unit paths\n"
  219. . " b - blacklist paths\n"
  220. . " ^ show all storage info\n"
  221. . " * uptime - shows Host uptime\n"
  222. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  223. . " * device - shows Host specific device info\n"
  224. . " + cd/dvd - list vm's with attached cd/dvd drives\n"
  225. . " o listall - list all available devices(use for listing purpose only)\n"
  226. . " DC specific :\n"
  227. . " * cpu - shows cpu info\n"
  228. . " + usage - CPU usage in percentage\n"
  229. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  230. . " + usagemhz - CPU usage in MHz\n"
  231. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  232. . " ^ all cpu info\n"
  233. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  234. . " * mem - shows mem info\n"
  235. . " + usage - mem usage in percentage\n"
  236. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  237. . " + usagemb - mem usage in MB\n"
  238. . " o quickstats - switch for query either PerfCounter values or Runtime info\n"
  239. . " + swap - swap mem usage in MB\n"
  240. . " + overhead - additional mem used by VM Server in MB\n"
  241. . " + overall - overall mem used by VM Server in MB\n"
  242. . " + memctl - mem used by VM memory control driver(vmmemctl) that controls ballooning\n"
  243. . " ^ all mem info(except overall and no thresholds)\n"
  244. . " * net - shows net info\n"
  245. . " + usage - overall network usage in KBps(Kilobytes per Second)\n"
  246. . " + receive - receive in KBps(Kilobytes per Second)\n"
  247. . " + send - send in KBps(Kilobytes per Second)\n"
  248. . " ^ all net info(except usage and no thresholds)\n"
  249. . " * io - shows disk io info\n"
  250. . " + aborted - aborted commands count\n"
  251. . " + resets - bus resets count\n"
  252. . " + read - read latency in ms (totalReadLatency.average)\n"
  253. . " + write - write latency in ms (totalWriteLatency.average)\n"
  254. . " + kernel - kernel latency in ms\n"
  255. . " + device - device latency in ms\n"
  256. . " + queue - queue latency in ms\n"
  257. . " ^ all disk io info\n"
  258. . " * vmfs - shows Datastore info\n"
  259. . " + (name) - free space info for datastore with name (name)\n"
  260. . " o used - output used space instead of free\n"
  261. . " o breif - list only alerting volumes\n"
  262. . " o regexp - whether to treat name as regexp\n"
  263. . " o blacklistregexp - whether to treat blacklist as regexp\n"
  264. . " b - blacklist VMFS's\n"
  265. . " T (value) - timeshift to detemine if we need to refresh\n"
  266. . " ^ all datastore info\n"
  267. . " o used - output used space instead of free\n"
  268. . " o breif - list only alerting volumes\n"
  269. . " o blacklistregexp - whether to treat blacklist as regexp\n"
  270. . " b - blacklist VMFS's\n"
  271. . " T (value) - timeshift to detemine if we need to refresh\n"
  272. . " * runtime - shows runtime info\n"
  273. . " + list(vm) - list of VMWare machines and their statuses\n"
  274. . " + listhost - list of VMWare esx host servers and their statuses\n"
  275. . " + listcluster - list of VMWare clusters and their statuses\n"
  276. . " + tools - VMWare Tools status\n"
  277. . " b - blacklist VM's\n"
  278. . " + status - overall object status (gray/green/red/yellow)\n"
  279. . " + issues - all issues for the host\n"
  280. . " b - blacklist issues\n"
  281. . " ^ all runtime info(except cluster and tools and no thresholds)\n"
  282. . " * recommendations - shows recommendations for cluster\n"
  283. . " + (name) - recommendations for cluster with name (name)\n"
  284. . " ^ all clusters recommendations\n"
  285. . " Cluster specific :\n"
  286. . " * cpu - shows cpu info\n"
  287. . " + usage - CPU usage in percentage\n"
  288. . " + usagemhz - CPU usage in MHz\n"
  289. . " ^ all cpu info\n"
  290. . " * mem - shows mem info\n"
  291. . " + usage - mem usage in percentage\n"
  292. . " + usagemb - mem usage in MB\n"
  293. . " + swap - swap mem usage in MB\n"
  294. . " o listvm - turn on/off output list of swapping VM's\n"
  295. . " + memctl - mem used by VM memory control driver(vmmemctl) that controls ballooning\n"
  296. . " o listvm - turn on/off output list of ballooning VM's\n"
  297. . " ^ all mem info(plus overhead and no thresholds)\n"
  298. . " * cluster - shows cluster services info\n"
  299. . " + effectivecpu - total available cpu resources of all hosts within cluster\n"
  300. . " + effectivemem - total amount of machine memory of all hosts in the cluster\n"
  301. . " + failover - VMWare HA number of failures that can be tolerated\n"
  302. . " + cpufainess - fairness of distributed cpu resource allocation\n"
  303. . " + memfainess - fairness of distributed mem resource allocation\n"
  304. . " ^ only effectivecpu and effectivemem values for cluster services\n"
  305. . " * runtime - shows runtime info\n"
  306. . " + list(vm) - list of VMWare machines in cluster and their statuses\n"
  307. . " + listhost - list of VMWare esx host servers in cluster and their statuses\n"
  308. . " + status - overall cluster status (gray/green/red/yellow)\n"
  309. . " + issues - all issues for the cluster\n"
  310. . " b - blacklist issues\n"
  311. . " ^ all cluster runtime info\n"
  312. . " * vmfs - shows Datastore info\n"
  313. . " + (name) - free space info for datastore with name (name)\n"
  314. . " o used - output used space instead of free\n"
  315. . " o breif - list only alerting volumes\n"
  316. . " o regexp - whether to treat name as regexp\n"
  317. . " o blacklistregexp - whether to treat blacklist as regexp\n"
  318. . " b - blacklist VMFS's\n"
  319. . " T (value) - timeshift to detemine if we need to refresh\n"
  320. . " ^ all datastore info\n"
  321. . " o used - output used space instead of free\n"
  322. . " o breif - list only alerting volumes\n"
  323. . " o blacklistregexp - whether to treat blacklist as regexp\n"
  324. . " b - blacklist VMFS's\n"
  325. . " T (value) - timeshift to detemine if we need to refresh\n"
  326. . "\n\nCopyright (c) 2008-2013 op5",
  327. timeout => 30,
  328. );
  329. $np->add_arg(
  330. spec => 'host|H=s',
  331. help => "-H, --host=<hostname>\n"
  332. . ' ESX or ESXi hostname.',
  333. required => 0,
  334. );
  335. $np->add_arg(
  336. spec => 'cluster|C=s',
  337. help => "-C, --cluster=<clustername>\n"
  338. . ' ESX or ESXi clustername.',
  339. required => 0,
  340. );
  341. $np->add_arg(
  342. spec => 'datacenter|D=s',
  343. help => "-D, --datacenter=<DCname>\n"
  344. . ' Datacenter hostname.',
  345. required => 0,
  346. );
  347. $np->add_arg(
  348. spec => 'name|N=s',
  349. help => "-N, --name=<vmname>\n"
  350. . ' Virtual machine name.',
  351. required => 0,
  352. );
  353. $np->add_arg(
  354. spec => 'username|u=s',
  355. help => "-u, --username=<username>\n"
  356. . ' Username to connect with.',
  357. required => 0,
  358. );
  359. $np->add_arg(
  360. spec => 'password|p=s',
  361. help => "-p, --password=<password>\n"
  362. . ' Password to use with the username.',
  363. required => 0,
  364. );
  365. $np->add_arg(
  366. spec => 'authfile|f=s',
  367. help => "-f, --authfile=<path>\n"
  368. . " Authentication file with login and password. File syntax :\n"
  369. . " username=<login>\n"
  370. . ' password=<password>',
  371. required => 0,
  372. );
  373. $np->add_arg(
  374. spec => 'warning|w=s',
  375. help => "-w, --warning=THRESHOLD\n"
  376. . " Warning threshold. See\n"
  377. . " http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n"
  378. . ' for the threshold format. By default, no threshold is set.',
  379. required => 0,
  380. );
  381. $np->add_arg(
  382. spec => 'critical|c=s',
  383. help => "-c, --critical=THRESHOLD\n"
  384. . " Critical threshold. See\n"
  385. . " http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n"
  386. . ' for the threshold format. By default, no threshold is set.',
  387. required => 0,
  388. );
  389. $np->add_arg(
  390. spec => 'command|l=s',
  391. help => "-l, --command=COMMAND\n"
  392. . ' Specify command type (CPU, MEM, NET, IO, VMFS, RUNTIME, ...)',
  393. required => 1,
  394. );
  395. $np->add_arg(
  396. spec => 'subcommand|s=s',
  397. help => "-s, --subcommand=SUBCOMMAND\n"
  398. . ' Specify subcommand',
  399. required => 0,
  400. );
  401. $np->add_arg(
  402. spec => 'sessionfile|S=s',
  403. help => "-S, --sessionfile=SESSIONFILE\n"
  404. . ' Specify a filename to store sessions for faster authentication',
  405. required => 0,
  406. );
  407. $np->add_arg(
  408. spec => 'exclude|x=s',
  409. help => "-x, --exclude=<black_list>\n"
  410. . ' Specify black list',
  411. required => 0,
  412. );
  413. $np->add_arg(
  414. spec => 'options|o=s',
  415. help => "-o, --options=<additional_options> \n"
  416. . ' Specify additional command options (quickstats, ...)',
  417. required => 0,
  418. );
  419. $np->add_arg(
  420. spec => 'timestamp|T=i',
  421. help => "-T, --timestamp=<timeshift> \n"
  422. . ' Timeshift in seconds that could fix issues with "Unknown error". Use values like 5, 10, 20, etc',
  423. required => 0,
  424. );
  425. $np->add_arg(
  426. spec => 'interval|i=s',
  427. help => "-i, --interval=<sampling period> \n"
  428. . " Sampling Period in seconds. Basic historic intervals: 300, 1800, 7200 or 86400. See config for any changes.\n"
  429. . " Supports literval values to autonegotiate interval value: r - realtime interval, h<number> - historical interval specified by position.\n"
  430. . ' Default value is 20 (realtime). Since cluster does not have realtime stats interval other than 20(default realtime) is mandatory.',
  431. required => 0,
  432. );
  433. $np->add_arg(
  434. spec => 'maxsamples|M=s',
  435. help => "-M, --maxsamples=<max sample count> \n"
  436. . " Maximum number of samples to retrieve. Max sample number is ignored for historic intervals.\n"
  437. . ' Default value is 1 (latest available sample). ',
  438. required => 0,
  439. );
  440. $np->add_arg(
  441. spec => 'trace=s',
  442. help => "--trace=<level> \n"
  443. . ' Set verbosity level of vSphere API request/respond trace',
  444. required => 0,
  445. );
  446. $np->add_arg(
  447. spec => 'generate_test=s',
  448. help => "--generate_test=<file> \n"
  449. . ' Generate a test case script from the executed command/subcommand and write it to <file>.'
  450. . ' If <file> is "stdout", the test case script is written to stdout instead.',
  451. default => 0,
  452. required => 0,
  453. );
  454. $np->getopts;
  455. my $host = $np->opts->host;
  456. my $cluster = $np->opts->cluster;
  457. my $datacenter = $np->opts->datacenter;
  458. my $vmname = $np->opts->name;
  459. my $username = $np->opts->username;
  460. my $password = $np->opts->password;
  461. my $authfile = $np->opts->authfile;
  462. my $warning = $np->opts->warning;
  463. my $critical = $np->opts->critical;
  464. my $command = $np->opts->command;
  465. my $subcommand = $np->opts->subcommand;
  466. my $sessionfile = $np->opts->sessionfile;
  467. my $blacklist = $np->opts->exclude;
  468. my $addopts = $np->opts->options;
  469. my $trace = $np->opts->trace;
  470. my $generate_test = $np->opts->generate_test;
  471. my $timeshift = $np->opts->timestamp;
  472. my $interval = $np->opts->interval;
  473. my $maxsamples = $np->opts->maxsamples;
  474. my $timeout = $np->opts->timeout;
  475. my $percw;
  476. my $percc;
  477. if ($generate_test) {
  478. if (uc($generate_test) ne "STDOUT") {
  479. -e $generate_test and die("cowardly refusing to write test case script to existing file ${generate_test}");
  480. }
  481. use LWP::UserAgent;
  482. my $cref = *LWP::UserAgent::request{CODE};
  483. {
  484. no warnings 'redefine';
  485. *LWP::UserAgent::request = sub {
  486. my $r = &{$cref}(@_); #$r is (hopefully) a SOAP response as returned by the VMware WS
  487. if (uc($generate_test) ne "STDOUT") {
  488. open TEST_SCRIPT, ">>", $generate_test;
  489. print TEST_SCRIPT $r->content . "\n!\n"; #print the response content to the target script. separate messages by '!' for easy parsing
  490. } else {
  491. print $r->content . "\n";
  492. }
  493. $r #pass it on
  494. };
  495. }
  496. }
  497. eval {
  498. require VMware::VIRuntime;
  499. } or Nagios::Plugin::Functions::nagios_exit(UNKNOWN, "Missing perl module VMware::VIRuntime. Download and install \'VMware vSphere SDK for Perl\', available at https://my.vmware.com/group/vmware/downloads\n $perl_module_instructions"); #This is, potentially, a lie. This might just as well fail if a dependency of VMware::VIRuntime is missing (i.e VIRuntime itself requires something which in turn fails).
  500. alarm($timeout) if $timeout;
  501. $output = "Unknown ERROR!";
  502. $result = CRITICAL;
  503. if (defined($subcommand))
  504. {
  505. $subcommand = undef if ($subcommand eq '');
  506. }
  507. if (defined($critical))
  508. {
  509. ($percc, $critical) = check_percantage($critical);
  510. $critical = undef if ($critical eq '');
  511. }
  512. if (defined($warning))
  513. {
  514. ($percw, $warning) = check_percantage($warning);
  515. $warning = undef if ($warning eq '');
  516. }
  517. $np->set_thresholds(critical => $critical, warning => $warning);
  518. $defperfargs = {};
  519. $defperfargs->{timeshift} = $timeshift if (defined($timeshift));
  520. $defperfargs->{interval} = $interval if (defined($interval));
  521. $defperfargs->{maxsamples} = $maxsamples if (defined($maxsamples));
  522. eval
  523. {
  524. die "Provide either Password/Username or Auth file or Session file\n" if ((!defined($password) || !defined($username) || defined($authfile)) && (defined($password) || defined($username) || !defined($authfile)) && (defined($password) || defined($username) || defined($authfile) || !defined($sessionfile)));
  525. die "Both threshold values must be the same units\n" if (($percw && !$percc && defined($critical)) || (!$percw && $percc && defined($warning)));
  526. if (defined($authfile))
  527. {
  528. open (AUTH_FILE, $authfile) || die "Unable to open auth file \"$authfile\"\n";
  529. while( <AUTH_FILE> ) {
  530. if(s/^[ \t]*username[ \t]*=//){
  531. s/^\s+//;s/\s+$//;
  532. $username = $_;
  533. }
  534. if(s/^[ \t]*password[ \t]*=//){
  535. s/^\s+//;s/\s+$//;
  536. $password = $_;
  537. }
  538. }
  539. die "Auth file must contain both username and password\n" if (!(defined($username) && defined($password)));
  540. }
  541. my $host_address;
  542. if (defined($datacenter))
  543. {
  544. $host_address = $datacenter;
  545. }
  546. elsif (defined($host))
  547. {
  548. $host_address = $host;
  549. }
  550. else
  551. {
  552. $np->nagios_exit(CRITICAL, "No Host or Datacenter specified");
  553. }
  554. $host_address .= ":443" if (index($host_address, ":") == -1);
  555. $host_address = "https://" . $host_address . "/sdk/webService";
  556. if (defined($sessionfile) and -e $sessionfile)
  557. {
  558. Opts::set_option("sessionfile", $sessionfile);
  559. eval {
  560. Util::connect($host_address, $username, $password);
  561. die "Connected host doesn't match reqested once\n" if (Opts::get_option("url") ne $host_address);
  562. };
  563. if ($@) {
  564. Opts::set_option("sessionfile", undef);
  565. Util::connect($host_address, $username, $password);
  566. }
  567. }
  568. else
  569. {
  570. Util::connect($host_address, $username, $password);
  571. }
  572. if (defined($sessionfile))
  573. {
  574. Vim::save_session(session_file => $sessionfile);
  575. }
  576. if (defined($trace))
  577. {
  578. $Util::tracelevel = $Util::tracelevel;
  579. $Util::tracelevel = $trace if (($trace =~ m/^\d$/) && ($trace >= 0) && ($trace <= 4));
  580. }
  581. $command = uc($command);
  582. if (defined($vmname))
  583. {
  584. if ($command eq "CPU")
  585. {
  586. ($result, $output) = vm_cpu_info($vmname, $np, local_uc($subcommand));
  587. }
  588. elsif ($command eq "MEM")
  589. {
  590. ($result, $output) = vm_mem_info($vmname, $np, local_uc($subcommand));
  591. }
  592. elsif ($command eq "NET")
  593. {
  594. ($result, $output) = vm_net_info($vmname, $np, local_uc($subcommand));
  595. }
  596. elsif ($command eq "IO")
  597. {
  598. ($result, $output) = vm_disk_io_info($vmname, $np, local_uc($subcommand));
  599. }
  600. elsif ($command eq "RUNTIME")
  601. {
  602. ($result, $output) = vm_runtime_info($vmname, $np, local_uc($subcommand));
  603. }
  604. else
  605. {
  606. $output = "Unknown HOST-VM command\n" . $np->opts->_help;
  607. $result = CRITICAL;
  608. }
  609. }
  610. elsif (defined($host))
  611. {
  612. my $esx;
  613. $esx = {name => $host} if (defined($datacenter));
  614. if ($command eq "CPU")
  615. {
  616. ($result, $output) = host_cpu_info($esx, $np, local_uc($subcommand), $addopts);
  617. }
  618. elsif ($command eq "MEM")
  619. {
  620. ($result, $output) = host_mem_info($esx, $np, local_uc($subcommand), $addopts);
  621. }
  622. elsif ($command eq "NET")
  623. {
  624. ($result, $output) = host_net_info($esx, $np, local_uc($subcommand));
  625. }
  626. elsif ($command eq "IO")
  627. {
  628. ($result, $output) = host_disk_io_info($esx, $np, local_uc($subcommand));
  629. }
  630. elsif ($command eq "VMFS")
  631. {
  632. ($result, $output) = host_list_vm_volumes_info($esx, $np, $subcommand, $blacklist, $percc || $percw, $addopts);
  633. }
  634. elsif ($command eq "RUNTIME")
  635. {
  636. ($result, $output) = host_runtime_info($esx, $np, local_uc($subcommand), $blacklist, $addopts);
  637. }
  638. elsif ($command eq "SERVICE")
  639. {
  640. ($result, $output) = host_service_info($esx, $np, $subcommand);
  641. }
  642. elsif ($command eq "STORAGE")
  643. {
  644. ($result, $output) = host_storage_info($esx, $np, local_uc($subcommand), $blacklist);
  645. }
  646. elsif ($command eq "UPTIME")
  647. {
  648. ($result, $output) = host_uptime_info($esx, $np, $addopts);
  649. }
  650. elsif ($command eq "DEVICE")
  651. {
  652. ($result, $output) = host_device_info($esx, $np, $subcommand, $addopts);
  653. }
  654. else
  655. {
  656. $output = "Unknown HOST command\n" . $np->opts->_help;
  657. $result = CRITICAL;
  658. }
  659. }
  660. elsif (defined($cluster))
  661. {
  662. if ($command eq "CPU")
  663. {
  664. ($result, $output) = cluster_cpu_info($cluster, $np, local_uc($subcommand));
  665. }
  666. elsif ($command eq "MEM")
  667. {
  668. ($result, $output) = cluster_mem_info($cluster, $np, local_uc($subcommand), $addopts);
  669. }
  670. elsif ($command eq "CLUSTER")
  671. {
  672. ($result, $output) = cluster_cluster_info($cluster, $np, local_uc($subcommand));
  673. }
  674. elsif ($command eq "VMFS")
  675. {
  676. ($result, $output) = cluster_list_vm_volumes_info($cluster, $np, $subcommand, $blacklist, $percc || $percw, $addopts);
  677. }
  678. elsif ($command eq "RUNTIME")
  679. {
  680. ($result, $output) = cluster_runtime_info($cluster, $np, local_uc($subcommand), $blacklist);
  681. }
  682. else
  683. {
  684. $output = "Unknown CLUSTER command\n" . $np->opts->_help;
  685. $result = CRITICAL;
  686. }
  687. }
  688. else
  689. {
  690. if ($command eq "RECOMMENDATIONS")
  691. {
  692. my $cluster_name;
  693. $cluster_name = {name => $subcommand} if (defined($subcommand));
  694. ($result, $output) = return_cluster_DRS_recommendations($np, $cluster_name);
  695. }
  696. elsif ($command eq "CPU")
  697. {
  698. ($result, $output) = dc_cpu_info($np, local_uc($subcommand), $addopts);
  699. }
  700. elsif ($command eq "MEM")
  701. {
  702. ($result, $output) = dc_mem_info($np, local_uc($subcommand), $addopts);
  703. }
  704. elsif ($command eq "NET")
  705. {
  706. ($result, $output) = dc_net_info($np, local_uc($subcommand));
  707. }
  708. elsif ($command eq "IO")
  709. {
  710. ($result, $output) = dc_disk_io_info($np, local_uc($subcommand));
  711. }
  712. elsif ($command eq "VMFS")
  713. {
  714. ($result, $output) = dc_list_vm_volumes_info($np, $subcommand, $blacklist, $percc || $percw, $addopts);
  715. }
  716. elsif ($command eq "RUNTIME")
  717. {
  718. ($result, $output) = dc_runtime_info($np, local_uc($subcommand), $blacklist);
  719. }
  720. else
  721. {
  722. $output = "Unknown HOST command\n" . $np->opts->_help;
  723. $result = CRITICAL;
  724. }
  725. }
  726. };
  727. if ($@)
  728. {
  729. if (uc(ref($@)) eq "HASH")
  730. {
  731. $output = $@->{msg};
  732. $result = $@->{code};
  733. }
  734. else
  735. {
  736. $output = $@ . "";
  737. $result = CRITICAL;
  738. }
  739. }
  740. Util::disconnect();
  741. if ($generate_test && uc($generate_test) ne 'STDOUT') {
  742. open TEST_SCRIPT, ">>", $generate_test;
  743. print TEST_SCRIPT "#" . $output . "\n";
  744. print TEST_SCRIPT "-" . $result;
  745. }
  746. $np->nagios_exit($result, $output);
  747. }
  748. main unless defined caller;
  749. #######################################################################################################################################################################
  750. sub get_key_metrices {
  751. my ($perfmgr_view, $group, @names) = @_;
  752. my $perfCounterInfo = $perfmgr_view->perfCounter;
  753. my @counters;
  754. die "Insufficient rights to access perfcounters\n" if (!defined($perfCounterInfo));
  755. foreach (@$perfCounterInfo) {
  756. if ($_->groupInfo->key eq $group) {
  757. my $cur_name = $_->nameInfo->key . "." . $_->rollupType->val;
  758. foreach my $index (0..@names-1)
  759. {
  760. if ($names[$index] =~ /$cur_name/)
  761. {
  762. $names[$index] =~ /(\w+).(\w+):*(.*)/;
  763. $counters[$index] = PerfMetricId->new(counterId => $_->key, instance => $3);
  764. }
  765. }
  766. }
  767. }
  768. return \@counters;
  769. }
  770. sub generic_performance_values {
  771. my ($views, $perfargs, $group, @list) = @_;
  772. my $counter = 0;
  773. my @values = ();
  774. my $amount = @list;
  775. my $perfMgr = $perfargs->{perfCounter};
  776. if (!defined($perfMgr)) {
  777. $perfMgr = Vim::get_view(mo_ref => Vim::get_service_content()->perfManager, properties => [ 'perfCounter' ]);
  778. $perfargs->{perfCounter} = $perfMgr;
  779. }
  780. my $metrices = get_key_metrices($perfMgr, $group, @list);
  781. my $maxsamples = defined($perfargs->{maxsamples}) ? $perfargs->{maxsamples} : 1; #default 1 sample
  782. my $interval = defined($perfargs->{interval}) ? $perfargs->{interval} : 20; #retrive RefreshRate as default value
  783. my $timestamp = $perfargs->{timestamp};
  784. my @perf_query_spec = ();
  785. if (defined($timestamp)) {
  786. my $timeshift = $perfargs->{timeshift};
  787. my ($sec,$min,$hour,$mday,$mon,$year) = gmtime($timestamp - $timeshift);
  788. my $startTime = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
  789. ($sec,$min,$hour,$mday,$mon,$year) = gmtime($timestamp);
  790. my $endTime = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
  791. if ($interval eq "r") {
  792. foreach (@$views) {
  793. my $summary = $perfMgr->QueryPerfProviderSummary(entity => $_);
  794. die "Realtime interval is not supported or not enabled\n" unless ($summary && $summary->currentSupported);
  795. $interval = $summary->refreshRate;
  796. push(@perf_query_spec, PerfQuerySpec->new(entity => $_, metricId => $metrices, format => 'csv', intervalId => $interval, maxSample => $maxsamples, startTime => $startTime, endTime => $endTime));
  797. }
  798. } elsif (substr($interval, 0, 1) eq "h") {
  799. my $index = substr($interval, 1, -1);
  800. foreach (@$views) {
  801. my $summary = $perfMgr->QueryPerfProviderSummary(entity => $_);
  802. die "Historical intervals are not supported\n" unless ($summary && $summary->summarySupported);
  803. my $historic_intervals = $perfMgr->historicalInterval;
  804. die "Historical interval [$index] is not present (max value " . @{$historic_intervals} . ")\n" unless (($index >= 0) && ($index < @{$historic_intervals}));
  805. my $perf_interval = $$historic_intervals[$index];
  806. die "Historical interval [$index] is disabled\n" unless ($perf_interval->enabled);
  807. $interval = $perf_interval->key;
  808. push(@perf_query_spec, PerfQuerySpec->new(entity => $_, metricId => $metrices, format => 'csv', intervalId => $interval, maxSample => $maxsamples, startTime => $startTime, endTime => $endTime));
  809. }
  810. } else {
  811. push(@perf_query_spec, PerfQuerySpec->new(entity => $_, metricId => $metrices, format => 'csv', intervalId => $interval, maxSample => $maxsamples, startTime => $startTime, endTime => $endTime)) foreach (@$views);
  812. }
  813. } else {
  814. if ($interval eq "r") {
  815. foreach (@$views) {
  816. my $summary = $perfMgr->QueryPerfProviderSummary(entity => $_);
  817. die "Realtime interval is not supported or not enabled\n" unless ($summary && $summary->currentSupported);
  818. $interval = $summary->refreshRate;
  819. push(@perf_query_spec, PerfQuerySpec->new(entity => $_, metricId => $metrices, format => 'csv', intervalId => $interval, maxSample => $maxsamples));
  820. }
  821. } elsif (substr($interval, 0, 1) eq "h") {
  822. my $index = substr($interval, 1, -1);
  823. foreach (@$views) {
  824. my $summary = $perfMgr->QueryPerfProviderSummary(entity => $_);
  825. die "Historical intervals are not supported\n" unless ($summary && $summary->summarySupported);
  826. my $historic_intervals = $perfMgr->historicalInterval;
  827. die "Historical interval [$index] is not present (max value " . @{$historic_intervals} . ")\n" unless (($index >= 0) && ($index < @{$historic_intervals}));
  828. my $perf_interval = $$historic_intervals[$index];
  829. die "Historical interval [$index] is disabled\n" unless ($perf_interval->enabled);
  830. $interval = $perf_interval->key;
  831. push(@perf_query_spec, PerfQuerySpec->new(entity => $_, metricId => $metrices, format => 'csv', intervalId => $interval, maxSample => $maxsamples));
  832. }
  833. } else {
  834. push(@perf_query_spec, PerfQuerySpec->new(entity => $_, metricId => $metrices, format => 'csv', intervalId => $interval, maxSample => $maxsamples)) foreach (@$views);
  835. }
  836. }
  837. my $perf_data = $perfMgr->QueryPerf(querySpec => \@perf_query_spec);
  838. $amount *= @$perf_data;
  839. while (@$perf_data)
  840. {
  841. my $unsorted = shift(@$perf_data)->value;
  842. my @host_values = ();
  843. foreach my $id (@$unsorted)
  844. {
  845. foreach my $index (0..@$metrices-1)
  846. {
  847. if ($id->id->counterId == $$metrices[$index]->counterId)
  848. {
  849. $counter++ if (!defined($host_values[$index]));
  850. $host_values[$index] = $id;
  851. }
  852. }
  853. }
  854. push(@values, \@host_values);
  855. }
  856. return undef if ($counter != $amount || $counter == 0);
  857. return \@values;
  858. }
  859. sub return_host_performance_values {
  860. my $values;
  861. my $host_name = shift(@_);
  862. my $perfargs = shift(@_);
  863. my $timeshift = $perfargs->{timeshift};
  864. my $host_view = Vim::find_entity_views(view_type => 'HostSystem', filter => $host_name, properties => (defined($timeshift) ? [ 'name', 'runtime.inMaintenanceMode', 'configManager.dateTimeSystem' ] : [ 'name', 'runtime.inMaintenanceMode']) ); # Added properties named argument.
  865. die "Runtime error\n" if (!defined($host_view));
  866. die "Host \"" . $$host_name{"name"} . "\" does not exist\n" if (!@$host_view);
  867. die {msg => ("NOTICE: \"" . $$host_view[0]->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($$host_view[0]->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  868. # Timestamp is required for some Hosts in vCenter(Datacenter), this could fix 'Unknown error' type of issues
  869. $perfargs->{timestamp} = str2time(Vim::get_view(mo_ref => $$host_view[0]->get_property('configManager.dateTimeSystem'))->QueryDateTime()) if (defined($timeshift));
  870. $values = generic_performance_values($host_view, $perfargs, @_);
  871. return undef if ($@);
  872. return ($host_view, $values);
  873. }
  874. sub return_host_vmware_performance_values {
  875. my $values;
  876. my $vmname = shift(@_);
  877. my $vm_view = Vim::find_entity_views(view_type => 'VirtualMachine', filter => {name => "$vmname"}, properties => [ 'name', 'runtime.powerState' ]);
  878. die "Runtime error\n" if (!defined($vm_view));
  879. die "VMware machine \"" . $vmname . "\" does not exist\n" if (!@$vm_view);
  880. die "VMware machine \"" . $vmname . "\" is not running. Current state is \"" . $$vm_view[0]->get_property('runtime.powerState')->val . "\"\n" if ($$vm_view[0]->get_property('runtime.powerState')->val ne "poweredOn");
  881. my $perfargs = shift(@_);
  882. $perfargs->{timestamp} = time() if (exists($perfargs->{timeshift}));
  883. $values = generic_performance_values($vm_view, $perfargs, @_);
  884. return $@ if ($@);
  885. return ($vm_view, $values);
  886. }
  887. sub return_dc_performance_values {
  888. my $values;
  889. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => [ 'name' ]);
  890. die "Runtime error\n" if (!defined($host_views));
  891. die "Datacenter does not contain any hosts\n" if (!@$host_views);
  892. my $perfargs = shift(@_);
  893. $perfargs->{timestamp} = time() if (exists($perfargs->{timeshift}));
  894. $values = generic_performance_values($host_views, $perfargs, @_);
  895. return undef if ($@);
  896. return ($host_views, $values);
  897. }
  898. sub return_cluster_performance_values {
  899. my $values;
  900. my $cluster_name = shift(@_);
  901. my $cluster_view = Vim::find_entity_views(view_type => 'ClusterComputeResource', filter => { name => "$cluster_name" }, properties => [ 'name' ]); # Added properties named argument.
  902. die "Runtime error\n" if (!defined($cluster_view));
  903. die "Cluster \"" . $cluster_name . "\" does not exist\n" if (!@$cluster_view);
  904. my $perfargs = shift(@_);
  905. die "Since cluster does not have realtime stats interval other than 20(default value) is mandatory\n" if (!exists($perfargs->{interval}));
  906. $perfargs->{timestamp} = time() if (exists($perfargs->{timeshift}));
  907. $values = generic_performance_values($cluster_view, $perfargs, @_);
  908. return undef if ($@);
  909. return $values;
  910. }
  911. # Temporary solution to overcome zeros in network output
  912. sub return_host_temporary_vc_4_1_network_performance_values {
  913. my @values;
  914. my ($host_name, $perfargs, @list) = @_;
  915. my $host_view = Vim::find_entity_views(view_type => 'HostSystem', filter => $host_name, properties => [ 'name', 'runtime.inMaintenanceMode', 'summary.config.product.version', 'configManager.dateTimeSystem' ]); # Added properties named argument.
  916. die "Runtime error\n" if (!defined($host_view));
  917. die "Host \"" . $$host_name{"name"} . "\" does not exist\n" if (!@$host_view);
  918. die {msg => ("NOTICE: \"" . $$host_view[0]->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($$host_view[0]->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  919. my $software_version = $$host_view[0]->get_property('summary.config.product.version');
  920. return undef if (substr($software_version, 0, 4) ne '4.1.');
  921. my $timeshift = $perfargs->{timeshift};
  922. my $interval = $perfargs->{interval};
  923. my $maxsamples = $perfargs->{maxsamples};
  924. my $timestamp = defined($timeshift) ? str2time(Vim::get_view(mo_ref => $$host_view[0]->get_property('configManager.dateTimeSystem'))->QueryDateTime()) : undef;
  925. my $perfMgr = Vim::get_view(mo_ref => Vim::get_service_content()->perfManager, properties => [ 'perfCounter' ]);
  926. my $metrices = get_key_metrices($perfMgr, 'net', @list);
  927. my $amount = @list;
  928. my @perf_query_spec = ();
  929. if (defined($timestamp)) {
  930. my ($sec,$min,$hour,$mday,$mon,$year) = gmtime($timestamp - $timeshift);
  931. my $endTime = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
  932. ($sec,$min,$hour,$mday,$mon,$year) = gmtime($timestamp - $timeshift);
  933. my $startTime = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
  934. push(@perf_query_spec, PerfQuerySpec->new(entity => $_, metricId => $metrices, format => 'csv', intervalId => $interval, startTime => $startTime, endtime => $endTime)) foreach (@$host_view);
  935. } else {
  936. push(@perf_query_spec, PerfQuerySpec->new(entity => $_, metricId => $metrices, format => 'csv', intervalId => $interval, maxSample => $maxsamples)) foreach (@$host_view);
  937. }
  938. my $perf_data = $perfMgr->QueryPerf(querySpec => \@perf_query_spec);
  939. $amount *= @$perf_data;
  940. my $counter = 0;
  941. while (@$perf_data)
  942. {
  943. my $unsorted = shift(@$perf_data)->value;
  944. my @host_values = ();
  945. foreach my $id (@$unsorted)
  946. {
  947. foreach my $index (0..@$metrices-1)
  948. {
  949. if ($id->id->counterId == $$metrices[$index]->counterId)
  950. {
  951. if (!defined($host_values[$index]))
  952. {
  953. $counter++;
  954. $host_values[$index] = bless({ 'value' => '0' }, "PerfMetricSeriesCSV");
  955. }
  956. $host_values[$index]{"value"} += convert_number($id->value) if ($id->id->instance ne '');
  957. }
  958. }
  959. }
  960. push(@values, \@host_values);
  961. }
  962. return undef if ($counter != $amount || $counter == 0 || $@);
  963. return ($host_view, \@values);
  964. }
  965. # Remove as soon as possible
  966. sub local_uc
  967. {
  968. my ($val) = shift(@_);
  969. return defined($val)?uc($val):undef;
  970. }
  971. sub simplify_number
  972. {
  973. my ($number, $cnt) = @_;
  974. $cnt = 2 if (!defined($cnt));
  975. return sprintf("%.${cnt}f", "$number");
  976. }
  977. sub convert_number
  978. {
  979. my @vals = split(/,/, shift(@_));
  980. my $res = 0;
  981. while (@vals) {
  982. my $value = pop(@vals);
  983. $value =~ s/^\s+//;
  984. $value =~ s/\s+$//;
  985. if (defined($value) && $value ne '') {
  986. return $value if ($value >= 0);
  987. $res = $value if ($res == 0);
  988. }
  989. }
  990. return $res;
  991. }
  992. sub check_percantage
  993. {
  994. my ($number) = shift(@_);
  995. my $perc = $number =~ s/\%//;
  996. return ($perc, $number);
  997. }
  998. sub check_health_state
  999. {
  1000. my ($state) = shift(@_);
  1001. my $res = UNKNOWN;
  1002. if (uc($state) eq "GREEN") {
  1003. $res = OK
  1004. } elsif (uc($state) eq "YELLOW") {
  1005. $res = WARNING;
  1006. } elsif (uc($state) eq "RED") {
  1007. $res = CRITICAL;
  1008. }
  1009. return $res;
  1010. }
  1011. sub format_issue {
  1012. my ($issue) = shift(@_);
  1013. my $output = '';
  1014. if (defined($issue->datacenter))
  1015. {
  1016. $output .= 'Datacenter "' . $issue->datacenter->name . '", ';
  1017. }
  1018. if (defined($issue->host))
  1019. {
  1020. $output .= 'Host "' . $issue->host->name . '", ';
  1021. }
  1022. if (defined($issue->vm))
  1023. {
  1024. $output .= 'VM "' . $issue->vm->name . '", ';
  1025. }
  1026. if (defined($issue->computeResource))
  1027. {
  1028. $output .= 'Compute Resource "' . $issue->computeResource->name . '", ';
  1029. }
  1030. if (exists($issue->{dvs}) && defined($issue->dvs))
  1031. {
  1032. # Since vSphere API 4.0
  1033. $output .= 'Virtual Switch "' . $issue->dvs->name . '", ';
  1034. }
  1035. if (exists($issue->{ds}) && defined($issue->ds))
  1036. {
  1037. # Since vSphere API 4.0
  1038. $output .= 'Datastore "' . $issue->ds->name . '", ';
  1039. }
  1040. if (exists($issue->{net}) && defined($issue->net))
  1041. {
  1042. # Since vSphere API 4.0
  1043. $output .= 'Network "' . $issue->net->name . '" ';
  1044. }
  1045. $output =~ s/, $/ /;
  1046. $output .= ": " . $issue->fullFormattedMessage;
  1047. $output .= "(caused by " . $issue->userName . ")" if ($issue->userName ne "");
  1048. return $output;
  1049. }
  1050. sub datastore_volumes_info
  1051. {
  1052. my ($datastore, $np, $subcommand, $blacklist, $perc, $addopts) = @_;
  1053. my $res = OK;
  1054. my $output = '';
  1055. my $usedflag;
  1056. my $briefflag;
  1057. my $regexpflag;
  1058. my $blackregexpflag;
  1059. $usedflag = $addopts =~ m/(^|\s|\t|,)\Qused\E($|\s|\t|,)/ if (defined($addopts));
  1060. $briefflag = $addopts =~ m/(^|\s|\t|,)\Qbrief\E($|\s|\t|,)/ if (defined($addopts));
  1061. $regexpflag = $addopts =~ m/(^|\s|\t|,)\Qregexp\E($|\s|\t|,)/ if (defined($addopts));
  1062. $blackregexpflag = $addopts =~ m/(^|\s|\t|,)\Qblacklistregexp\E($|\s|\t|,)/ if (defined($addopts));
  1063. die "Blacklist is supported only in generic check or regexp subcheck\n" if (defined($subcommand) && defined($blacklist) && !defined($regexpflag));
  1064. if (defined($regexpflag) && defined($subcommand))
  1065. {
  1066. eval
  1067. {
  1068. qr{$subcommand};
  1069. };
  1070. if ($@)
  1071. {
  1072. $@ =~ s/ at.*line.*\.//;
  1073. die $@;
  1074. }
  1075. }
  1076. my $state;
  1077. foreach my $ref_store (@{$datastore})
  1078. {
  1079. my $store = Vim::get_view(mo_ref => $ref_store, properties => ['summary', 'info']);
  1080. my $name = $store->summary->name;
  1081. if (!defined($subcommand) || ($name eq $subcommand) || (defined($regexpflag) && $name =~ /$subcommand/))
  1082. {
  1083. if (defined($blacklist))
  1084. {
  1085. next if ($blackregexpflag?$name =~ /$blacklist/:$blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  1086. }
  1087. if ($store->summary->accessible)
  1088. {
  1089. $store->RefreshDatastoreStorageInfo() if ($store->can("RefreshDatastoreStorageInfo") && exists($store->info->{timestamp}) && $defperfargs->{timeshift} && (time() - str2time($store->info->timestamp) > $defperfargs->{timeshift}));
  1090. my $value1 = simplify_number(convert_number($store->summary->freeSpace) / 1024 / 1024);
  1091. my $value2 = convert_number($store->summary->capacity);
  1092. $value2 = simplify_number(convert_number($store->info->freeSpace) / $value2 * 100) if ($value2 > 0);
  1093. if ($usedflag)
  1094. {
  1095. $value1 = simplify_number(convert_number($store->summary->capacity) / 1024 / 1024) - $value1;
  1096. $value2 = 100 - $value2;
  1097. }
  1098. $state = $np->check_threshold(check => $perc?$value2:$value1);
  1099. $res = Nagios::Plugin::Functions::max_state($res, $state);
  1100. $np->add_perfdata(label => $name, value => $perc?$value2:$value1, uom => $perc?'%':'MB', threshold => $np->threshold);
  1101. $output .= "'$name'" . ($usedflag ? "(used)" : "(free)") . "=". $value1 . " MB (" . $value2 . "%), " if (!$briefflag || $state != OK);
  1102. }
  1103. else
  1104. {
  1105. $res = CRITICAL;
  1106. $output .= "'$name' is not accessible, ";
  1107. }
  1108. last if (!$regexpflag && defined($subcommand) && ($name eq $subcommand));
  1109. $blacklist .= $blackregexpflag?"|^$name\$":",$name";
  1110. }
  1111. }
  1112. if ($output)
  1113. {
  1114. chop($output);
  1115. chop($output);
  1116. $output = "Storages : " . $output;
  1117. }
  1118. else
  1119. {
  1120. if ($briefflag)
  1121. {
  1122. $output = "There are no alerts";
  1123. }
  1124. else
  1125. {
  1126. $res = WARNING;
  1127. $output = defined($subcommand)?$regexpflag? "No matching volumes for regexp \"$subcommand\" found":"No volume named \"$subcommand\" found":"There are no volumes";
  1128. }
  1129. }
  1130. return ($res, $output);
  1131. }
  1132. #=====================================================================| HOST |============================================================================#
  1133. sub host_cpu_info
  1134. {
  1135. my ($host, $np, $subcommand, $addopts) = @_;
  1136. my $res = CRITICAL;
  1137. my $output = 'HOST CPU Unknown error';
  1138. my $quickStats;
  1139. $quickStats = $addopts =~ m/(^|\s|\t|,)\Qquickstats\E($|\s|\t|,)/ if (defined($addopts));
  1140. if (defined($subcommand))
  1141. {
  1142. if ($subcommand eq "USAGE")
  1143. {
  1144. my $value;
  1145. if (defined($quickStats))
  1146. {
  1147. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'runtime.inMaintenanceMode', 'summary.hardware', 'summary.quickStats']);
  1148. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  1149. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  1150. $values = $host_view->get_property('summary.quickStats');
  1151. my $hardinfo = $host_view->get_property('summary.hardware');
  1152. $value = simplify_number($values->overallCpuUsage / ($hardinfo->numCpuCores * $hardinfo->cpuMhz) * 100) if exists($values->{overallCpuUsage}) && defined($hardinfo);
  1153. }
  1154. else
  1155. {
  1156. $values = return_host_performance_values($host, $defperfargs, 'cpu', ('usage.average'));
  1157. $value = simplify_number(convert_number($$values[0][0]->value) * 0.01) if (defined($values));
  1158. }
  1159. if (defined($value))
  1160. {
  1161. $np->add_perfdata(label => "cpu_usage", value => $value, uom => '%', threshold => $np->threshold);
  1162. $output = "cpu usage=" . $value . " %";
  1163. $res = $np->check_threshold(check => $value);
  1164. }
  1165. }
  1166. elsif ($subcommand eq "USAGEMHZ")
  1167. {
  1168. my $value;
  1169. if (defined($quickStats))
  1170. {
  1171. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'runtime.inMaintenanceMode', 'summary.quickStats']);
  1172. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  1173. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  1174. $values = $host_view->get_property('summary.quickStats');
  1175. $value = simplify_number($values->overallCpuUsage) if exists($values->{overallCpuUsage});
  1176. }
  1177. else
  1178. {
  1179. $values = return_host_performance_values($host, $defperfargs, 'cpu', ('usagemhz.average'));
  1180. $value = simplify_number(convert_number($$values[0][0]->value)) if (defined($values));
  1181. }
  1182. if (defined($value))
  1183. {
  1184. $np->add_perfdata(label => "cpu_usagemhz", value => $value, uom => 'MHz', threshold => $np->threshold);
  1185. $output = "cpu usagemhz=" . $value . " MHz";
  1186. $res = $np->check_threshold(check => $value);
  1187. }
  1188. }
  1189. else
  1190. {
  1191. $res = CRITICAL;
  1192. $output = "HOST CPU - unknown subcommand\n" . $np->opts->_help;
  1193. }
  1194. }
  1195. else
  1196. {
  1197. my $value1;
  1198. my $value2;
  1199. if (defined($quickStats))
  1200. {
  1201. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'runtime.inMaintenanceMode', 'summary.hardware', 'summary.quickStats']);
  1202. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  1203. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  1204. $values = $host_view->get_property('summary.quickStats');
  1205. my $hardinfo = $host_view->get_property('summary.hardware');
  1206. if (exists($values->{overallCpuUsage}) && defined($hardinfo))
  1207. {
  1208. $value1 = simplify_number($values->overallCpuUsage);
  1209. $value2 = simplify_number($values->overallCpuUsage / ($hardinfo->numCpuCores * $hardinfo->cpuMhz) * 100);
  1210. }
  1211. }
  1212. else
  1213. {
  1214. $values = return_host_performance_values($host, $defperfargs, 'cpu', ('usagemhz.average', 'usage.average'));
  1215. if ($values) {
  1216. $value1 = simplify_number(convert_number($$values[0][0]->value));
  1217. $value2 = simplify_number(convert_number($$values[0][1]->value) * 0.01);
  1218. }
  1219. }
  1220. if (defined($value1) && defined($value2))
  1221. {
  1222. $np->add_perfdata(label => "cpu_usagemhz", value => $value1, uom => 'MHz', threshold => $np->threshold);
  1223. $np->add_perfdata(label => "cpu_usage", value => $value2, uom => '%', threshold => $np->threshold);
  1224. $res = OK;
  1225. $output = "cpu usage=" . $value1 . " MHz (" . $value2 . "%)";
  1226. }
  1227. }
  1228. return ($res, $output);
  1229. }
  1230. sub host_mem_info
  1231. {
  1232. my ($host, $np, $subcommand, $addopts) = @_;
  1233. my $res = CRITICAL;
  1234. my $output = 'HOST MEM Unknown error';
  1235. my $quickStats;
  1236. my $outputlist;
  1237. $quickStats = $addopts =~ m/(^|\s|\t|,)\Qquickstats\E($|\s|\t|,)/ if (defined($addopts));
  1238. $outputlist = $addopts =~ m/(^|\s|\t|,)\Qlistvm\E($|\s|\t|,)/ if (defined($addopts));
  1239. if (defined($subcommand))
  1240. {
  1241. if ($subcommand eq "USAGE")
  1242. {
  1243. my $value;
  1244. if (defined($quickStats))
  1245. {
  1246. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'runtime.inMaintenanceMode', 'summary.hardware', 'summary.quickStats']);
  1247. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  1248. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  1249. $values = $host_view->get_property('summary.quickStats');
  1250. my $hardinfo = $host_view->get_property('summary.hardware');
  1251. $value = simplify_number($values->overallMemoryUsage / ($hardinfo->memorySize / 1024 / 1024) * 100) if exists($values->{overallMemoryUsage}) && defined($hardinfo);
  1252. }
  1253. else
  1254. {
  1255. $values = return_host_performance_values($host, $defperfargs, 'mem', ('usage.average'));
  1256. $value = simplify_number(convert_number($$values[0][0]->value) * 0.01) if (defined($values));
  1257. }
  1258. if (defined($value))
  1259. {
  1260. $np->add_perfdata(label => "mem_usage", value => $value, uom => '%', threshold => $np->threshold);
  1261. $output = "mem usage=" . $value . " %";
  1262. $res = $np->check_threshold(check => $value);
  1263. }
  1264. }
  1265. elsif ($subcommand eq "USAGEMB")
  1266. {
  1267. my $value;
  1268. if (defined($quickStats))
  1269. {
  1270. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'runtime.inMaintenanceMode', 'summary.quickStats']);
  1271. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  1272. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  1273. $values = $host_view->get_property('summary.quickStats');
  1274. $value = simplify_number($values->overallMemoryUsage) if exists($values->{overallMemoryUsage});
  1275. }
  1276. else
  1277. {
  1278. $values = return_host_performance_values($host, $defperfargs, 'mem', ('consumed.average'));
  1279. $value = simplify_number(convert_number($$values[0][0]->value) / 1024) if (defined($values));
  1280. }
  1281. if (defined($value))
  1282. {
  1283. $np->add_perfdata(label => "mem_usagemb", value => $value, uom => 'MB', threshold => $np->threshold);
  1284. $output = "mem usage=" . $value . " MB";
  1285. $res = $np->check_threshold(check => $value);
  1286. }
  1287. }
  1288. elsif ($subcommand eq "SWAP")
  1289. {
  1290. my $host_view;
  1291. ($host_view, $values) = return_host_performance_values($host, $defperfargs, 'mem', ('swapused.average'));
  1292. if (defined($values))
  1293. {
  1294. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  1295. $np->add_perfdata(label => "mem_swap", value => $value, uom => 'MB', threshold => $np->threshold);
  1296. $output = "swap usage=" . $value . " MB: ";
  1297. $res = $np->check_threshold(check => $value);
  1298. if ($res != OK && $outputlist)
  1299. {
  1300. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $$host_view[0], properties => ['name', 'runtime.powerState']);
  1301. die "Runtime error\n" if (!defined($vm_views));
  1302. die "There are no VMs.\n" if (!@$vm_views);
  1303. my @vms = ();
  1304. foreach my $vm (@$vm_views)
  1305. {
  1306. push(@vms, $vm) if ($vm->get_property('runtime.powerState')->val eq "poweredOn");
  1307. }
  1308. $values = generic_performance_values(\@vms, $defperfargs, 'mem', ('swapped.average'));
  1309. if (defined($values))
  1310. {
  1311. foreach my $index (0..@vms-1) {
  1312. my $value = simplify_number(convert_number($$values[$index][0]->value) / 1024);
  1313. $output .= $vms[$index]->name . " (" . $value . "MB), " if ($value > 0);
  1314. }
  1315. }
  1316. }
  1317. chop($output);
  1318. chop($output);
  1319. }
  1320. }
  1321. elsif ($subcommand eq "OVERHEAD")
  1322. {
  1323. $values = return_host_performance_values($host, $defperfargs, 'mem', ('overhead.average'));
  1324. if (defined($values))
  1325. {
  1326. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  1327. $np->add_perfdata(label => "mem_overhead", value => $value, uom => 'MB', threshold => $np->threshold);
  1328. $output = "overhead=" . $value . " MB";
  1329. $res = $np->check_threshold(check => $value);
  1330. }
  1331. }
  1332. elsif ($subcommand eq "OVERALL")
  1333. {
  1334. $values = return_host_performance_values($host, $defperfargs, 'mem', ('consumed.average', 'overhead.average'));
  1335. if (defined($values))
  1336. {
  1337. my $value = simplify_number((convert_number($$values[0][0]->value) + convert_number($$values[0][1]->value)) / 1024);
  1338. $np->add_perfdata(label => "mem_overhead", value => $value, uom => 'MB', threshold => $np->threshold);
  1339. $output = "overall=" . $value . " MB";
  1340. $res = $np->check_threshold(check => $value);
  1341. }
  1342. }
  1343. elsif ($subcommand eq "MEMCTL")
  1344. {
  1345. my $host_view;
  1346. ($host_view, $values) = return_host_performance_values($host, $defperfargs, 'mem', ('vmmemctl.average'));
  1347. if (defined($values))
  1348. {
  1349. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  1350. $np->add_perfdata(label => "mem_memctl", value => $value, uom => 'MB', threshold => $np->threshold);
  1351. $output = "memctl=" . $value . " MB: ";
  1352. $res = $np->check_threshold(check => $value);
  1353. if ($res != OK && $outputlist)
  1354. {
  1355. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $$host_view[0], properties => ['name', 'runtime.powerState']);
  1356. die "Runtime error\n" if (!defined($vm_views));
  1357. die "There are no VMs.\n" if (!@$vm_views);
  1358. my @vms = ();
  1359. foreach my $vm (@$vm_views)
  1360. {
  1361. push(@vms, $vm) if ($vm->get_property('runtime.powerState')->val eq "poweredOn");
  1362. }
  1363. $values = generic_performance_values(\@vms, $defperfargs, 'mem', ('vmmemctl.average'));
  1364. if (defined($values))
  1365. {
  1366. foreach my $index (0..@vms-1) {
  1367. my $value = simplify_number(convert_number($$values[$index][0]->value) / 1024);
  1368. $output .= $vms[$index]->name . " (" . $value . "MB), " if ($value > 0);
  1369. }
  1370. }
  1371. }
  1372. chop($output);
  1373. chop($output);
  1374. }
  1375. }
  1376. else
  1377. {
  1378. $res = CRITICAL;
  1379. $output = "HOST MEM - unknown subcommand\n" . $np->opts->_help;
  1380. }
  1381. }
  1382. else
  1383. {
  1384. $values = return_host_performance_values($host, $defperfargs, 'mem', ('consumed.average', 'usage.average', 'overhead.average', 'swapused.average', 'vmmemctl.average'));
  1385. if (defined($values))
  1386. {
  1387. my $value1 = simplify_number(convert_number($$values[0][0]->value) / 1024);
  1388. my $value2 = simplify_number(convert_number($$values[0][1]->value) * 0.01);
  1389. my $value3 = simplify_number(convert_number($$values[0][2]->value) / 1024);
  1390. my $value4 = simplify_number(convert_number($$values[0][3]->value) / 1024);
  1391. my $value5 = simplify_number(convert_number($$values[0][4]->value) / 1024);
  1392. $np->add_perfdata(label => "mem_usagemb", value => $value1, uom => 'MB', threshold => $np->threshold);
  1393. $np->add_perfdata(label => "mem_usage", value => $value2, uom => '%', threshold => $np->threshold);
  1394. $np->add_perfdata(label => "mem_overhead", value => $value3, uom => 'MB', threshold => $np->threshold);
  1395. $np->add_perfdata(label => "mem_swap", value => $value4, uom => 'MB', threshold => $np->threshold);
  1396. $np->add_perfdata(label => "mem_memctl", value => $value5, uom => 'MB', threshold => $np->threshold);
  1397. $res = OK;
  1398. $output = "mem usage=" . $value1 . " MB (" . $value2 . "%), overhead=" . $value3 . " MB, swapped=" . $value4 . " MB, memctl=" . $value5 . " MB";
  1399. }
  1400. }
  1401. return ($res, $output);
  1402. }
  1403. sub host_net_info
  1404. {
  1405. my ($host, $np, $subcommand) = @_;
  1406. my $res = CRITICAL;
  1407. my $output = 'HOST NET Unknown error';
  1408. if (defined($subcommand))
  1409. {
  1410. if ($subcommand eq "USAGE")
  1411. {
  1412. $values = return_host_temporary_vc_4_1_network_performance_values($host, $defperfargs, ('received.average:*', 'transmitted.average:*'));
  1413. if ($values)
  1414. {
  1415. $$values[0][0]{"value"} += $$values[0][1]{"value"};
  1416. }
  1417. else
  1418. {
  1419. $values = return_host_performance_values($host, $defperfargs, 'net', ('usage.average'));
  1420. }
  1421. if (defined($values))
  1422. {
  1423. my $value = simplify_number(convert_number($$values[0][0]->value));
  1424. $np->add_perfdata(label => "net_usage", value => $value, uom => 'KBps', threshold => $np->threshold);
  1425. $output = "net usage=" . $value . " KBps";
  1426. $res = $np->check_threshold(check => $value);
  1427. }
  1428. }
  1429. elsif ($subcommand eq "RECEIVE")
  1430. {
  1431. $values = return_host_temporary_vc_4_1_network_performance_values($host, $defperfargs, ('received.average:*'));
  1432. $values = return_host_performance_values($host, $defperfargs, 'net', ('received.average')) if (!$values);
  1433. if (defined($values))
  1434. {
  1435. my $value = simplify_number(convert_number($$values[0][0]->value));
  1436. $np->add_perfdata(label => "net_receive", value => $value, uom => 'KBps', threshold => $np->threshold);
  1437. $output = "net receive=" . $value . " KBps";
  1438. $res = $np->check_threshold(check => $value);
  1439. }
  1440. }
  1441. elsif ($subcommand eq "SEND")
  1442. {
  1443. $values = return_host_temporary_vc_4_1_network_performance_values($host, $defperfargs, ('transmitted.average:*'));
  1444. $values = return_host_performance_values($host, $defperfargs, 'net', ('transmitted.average')) if (!$values);
  1445. if (defined($values))
  1446. {
  1447. my $value = simplify_number(convert_number($$values[0][0]->value));
  1448. $np->add_perfdata(label => "net_send", value => $value, uom => 'KBps', threshold => $np->threshold);
  1449. $output = "net send=" . $value . " KBps";
  1450. $res = $np->check_threshold(check => $value);
  1451. }
  1452. }
  1453. elsif ($subcommand eq "NIC")
  1454. {
  1455. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'configManager.networkSystem', 'runtime.inMaintenanceMode']);
  1456. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  1457. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  1458. my $network_system = Vim::get_view(mo_ref => $host_view->get_property('configManager.networkSystem') , properties => ['networkInfo']);
  1459. $network_system->update_view_data(['networkInfo']);
  1460. my $network_config = $network_system->networkInfo;
  1461. die "Host \"" . $$host{"name"} . "\" has no network info in the API.\n" if (!defined($network_config));
  1462. $output = "";
  1463. $res = OK;
  1464. my $OKCount = 0;
  1465. my $BadCount = 0;
  1466. my @switches = ();
  1467. # create a hash of NIC info to facilitate easy lookups
  1468. my %NIC = ();
  1469. foreach (@{$network_config->pnic})
  1470. {
  1471. $NIC{$_->key} = $_;
  1472. }
  1473. push(@switches, $network_config->vswitch) if (exists($network_config->{vswitch}));
  1474. push(@switches, $network_config->proxySwitch) if (exists($network_config->{proxySwitch}));
  1475. # see which NICs are actively part of a switch
  1476. foreach my $switch (@switches)
  1477. {
  1478. foreach (@{$switch})
  1479. {
  1480. # get list of physical nics
  1481. if (defined($_->pnic)){
  1482. foreach my $nic_key (@{$_->pnic})
  1483. {
  1484. if (!defined($NIC{$nic_key}->linkSpeed))
  1485. {
  1486. $output .= ", " if ($output);
  1487. $output .= $NIC{$nic_key}->device . " is unplugged";
  1488. $res = CRITICAL;
  1489. $BadCount++;
  1490. }
  1491. else
  1492. {
  1493. $OKCount++;
  1494. }
  1495. }
  1496. }
  1497. }
  1498. }
  1499. if (!$BadCount)
  1500. {
  1501. $output = "All $OKCount NICs are connected";
  1502. }
  1503. else
  1504. {
  1505. $output = $BadCount ."/" . ($BadCount + $OKCount) . " NICs are disconnected: " . $output;
  1506. }
  1507. $np->add_perfdata(label => "OK_NICs", value => $OKCount);
  1508. $np->add_perfdata(label => "Bad_NICs", value => $BadCount);
  1509. }
  1510. else
  1511. {
  1512. $res = CRITICAL;
  1513. $output = "HOST NET - unknown subcommand\n" . $np->opts->_help;
  1514. }
  1515. }
  1516. else
  1517. {
  1518. my $host_view;
  1519. ($host_view, $values) = return_host_performance_values($host, $defperfargs, 'net', ('received.average', 'transmitted.average'));
  1520. $output = '';
  1521. if (defined($values))
  1522. {
  1523. my $value1 = simplify_number(convert_number($$values[0][0]->value));
  1524. my $value2 = simplify_number(convert_number($$values[0][1]->value));
  1525. $host_view = $$host_view[0];
  1526. $host_view->update_view_data(['configManager.networkSystem']);
  1527. my $network_system = Vim::get_view(mo_ref => $host_view->get_property('configManager.networkSystem') , properties => ['networkInfo']);
  1528. $network_system->update_view_data(['networkInfo']);
  1529. my $network_config = $network_system->networkInfo;
  1530. if (defined($network_config))
  1531. {
  1532. my $OKCount = 0;
  1533. my $BadCount = 0;
  1534. # create a hash of NIC info to facilitate easy lookups
  1535. my %NIC = ();
  1536. foreach (@{$network_config->pnic})
  1537. {
  1538. $NIC{$_->key} = $_;
  1539. }
  1540. my $nic_output = '';
  1541. my @switches = ();
  1542. push(@switches, $network_config->vswitch) if (exists($network_config->{vswitch}));
  1543. push(@switches, $network_config->proxySwitch) if (exists($network_config->{proxySwitch}));
  1544. # see which NICs are actively part of a switch
  1545. foreach my $switch (@switches)
  1546. {
  1547. foreach (@{$switch})
  1548. {
  1549. # get list of physical nics
  1550. if (defined($_->pnic)){
  1551. foreach my $nic_key (@{$_->pnic})
  1552. {
  1553. if (!defined($NIC{$nic_key}->linkSpeed))
  1554. {
  1555. $nic_output .= ", " if ($output);
  1556. $nic_output .= $NIC{$nic_key}->device . " is unplugged";
  1557. $res = CRITICAL;
  1558. $BadCount++;
  1559. }
  1560. else
  1561. {
  1562. $OKCount++;
  1563. }
  1564. }
  1565. }
  1566. }
  1567. }
  1568. $res = OK;
  1569. $output = "net receive=" . $value1 . " KBps, send=" . $value2 . " KBps, ";
  1570. $np->add_perfdata(label => "net_receive", value => $value1, uom => 'KBps', threshold => $np->threshold);
  1571. $np->add_perfdata(label => "net_send", value => $value2, uom => 'KBps', threshold => $np->threshold);
  1572. if (!$BadCount)
  1573. {
  1574. $output .= "all $OKCount NICs are connected";
  1575. }
  1576. else
  1577. {
  1578. $output .= $BadCount ."/" . ($BadCount + $OKCount) . " NICs are disconnected: " . $nic_output;
  1579. }
  1580. $np->add_perfdata(label => "OK_NICs", value => $OKCount);
  1581. $np->add_perfdata(label => "Bad_NICs", value => $BadCount);
  1582. }
  1583. else
  1584. {
  1585. $output = "NIC data is not available";
  1586. }
  1587. }
  1588. }
  1589. return ($res, $output);
  1590. }
  1591. sub host_disk_io_info
  1592. {
  1593. my ($host, $np, $subcommand) = @_;
  1594. my $res = CRITICAL;
  1595. my $output = 'HOST IO Unknown error';
  1596. if (defined($subcommand))
  1597. {
  1598. if ($subcommand eq "ABORTED")
  1599. {
  1600. $values = return_host_performance_values($host, $defperfargs, 'disk', ('commandsAborted.summation:*'));
  1601. if (defined($values))
  1602. {
  1603. my $value = simplify_number(convert_number($$values[0][0]->value), 0);
  1604. $np->add_perfdata(label => "io_aborted", value => $value, threshold => $np->threshold);
  1605. $output = "io commands aborted=" . $value;
  1606. $res = $np->check_threshold(check => $value);
  1607. }
  1608. }
  1609. elsif ($subcommand eq "RESETS")
  1610. {
  1611. $values = return_host_performance_values($host, $defperfargs, 'disk', ('busResets.summation:*'));
  1612. if (defined($values))
  1613. {
  1614. my $value = simplify_number(convert_number($$values[0][0]->value), 0);
  1615. $np->add_perfdata(label => "io_busresets", value => $value, threshold => $np->threshold);
  1616. $output = "io bus resets=" . $value;
  1617. $res = $np->check_threshold(check => $value);
  1618. }
  1619. }
  1620. elsif ($subcommand eq "READ")
  1621. {
  1622. $values = return_host_performance_values($host, $defperfargs, 'disk', ('totalReadLatency.average:*'));
  1623. if (defined($values))
  1624. {
  1625. my $value = simplify_number(convert_number($$values[0][0]->value), 0);
  1626. $np->add_perfdata(label => "io_read", value => $value, uom => 'ms', threshold => $np->threshold);
  1627. $output = "io read latency=" . $value . " ms";
  1628. $res = $np->check_threshold(check => $value);
  1629. }
  1630. }
  1631. elsif ($subcommand eq "WRITE")
  1632. {
  1633. $values = return_host_performance_values($host, $defperfargs, 'disk', ('totalWriteLatency.average:*'));
  1634. if (defined($values))
  1635. {
  1636. my $value = simplify_number(convert_number($$values[0][0]->value), 0);
  1637. $np->add_perfdata(label => "io_write", value => $value, uom => 'ms', threshold => $np->threshold);
  1638. $output = "io write latency=" . $value . " ms";
  1639. $res = $np->check_threshold(check => $value);
  1640. }
  1641. }
  1642. elsif ($subcommand eq "KERNEL")
  1643. {
  1644. $values = return_host_performance_values($host, $defperfargs, 'disk', ('kernelLatency.average:*'));
  1645. if (defined($values))
  1646. {
  1647. my $value = simplify_number(convert_number($$values[0][0]->value), 0);
  1648. $np->add_perfdata(label => "io_kernel", value => $value, uom => 'ms', threshold => $np->threshold);
  1649. $output = "io kernel latency=" . $value . " ms";
  1650. $res = $np->check_threshold(check => $value);
  1651. }
  1652. }
  1653. elsif ($subcommand eq "DEVICE")
  1654. {
  1655. $values = return_host_performance_values($host, $defperfargs, 'disk', ('deviceLatency.average:*'));
  1656. if (defined($values))
  1657. {
  1658. my $value = simplify_number(convert_number($$values[0][0]->value), 0);
  1659. $np->add_perfdata(label => "io_device", value => $value, uom => 'ms', threshold => $np->threshold);
  1660. $output = "io device latency=" . $value . " ms";
  1661. $res = $np->check_threshold(check => $value);
  1662. }
  1663. }
  1664. elsif ($subcommand eq "QUEUE")
  1665. {
  1666. $values = return_host_performance_values($host, $defperfargs, 'disk', ('queueLatency.average:*'));
  1667. if (defined($values))
  1668. {
  1669. my $value = simplify_number(convert_number($$values[0][0]->value), 0);
  1670. $np->add_perfdata(label => "io_queue", value => $value, uom => 'ms', threshold => $np->threshold);
  1671. $output = "io queue latency=" . $value . " ms";
  1672. $res = $np->check_threshold(check => $value);
  1673. }
  1674. }
  1675. else
  1676. {
  1677. $res = CRITICAL;
  1678. $output = "HOST IO - unknown subcommand\n" . $np->opts->_help;
  1679. }
  1680. }
  1681. else
  1682. {
  1683. $values = return_host_performance_values($host, $defperfargs, 'disk', ('commandsAborted.summation:*', 'busResets.summation:*', 'totalReadLatency.average:*', 'totalWriteLatency.average:*', 'kernelLatency.average:*', 'deviceLatency.average:*', 'queueLatency.average:*'));
  1684. if (defined($values))
  1685. {
  1686. my $value1 = simplify_number(convert_number($$values[0][0]->value), 0);
  1687. my $value2 = simplify_number(convert_number($$values[0][1]->value), 0);
  1688. my $value3 = simplify_number(convert_number($$values[0][2]->value), 0);
  1689. my $value4 = simplify_number(convert_number($$values[0][3]->value), 0);
  1690. my $value5 = simplify_number(convert_number($$values[0][4]->value), 0);
  1691. my $value6 = simplify_number(convert_number($$values[0][5]->value), 0);
  1692. my $value7 = simplify_number(convert_number($$values[0][6]->value), 0);
  1693. $np->add_perfdata(label => "io_aborted", value => $value1, threshold => $np->threshold);
  1694. $np->add_perfdata(label => "io_busresets", value => $value2, threshold => $np->threshold);
  1695. $np->add_perfdata(label => "io_read", value => $value3, uom => 'ms', threshold => $np->threshold);
  1696. $np->add_perfdata(label => "io_write", value => $value4, uom => 'ms', threshold => $np->threshold);
  1697. $np->add_perfdata(label => "io_kernel", value => $value5, uom => 'ms', threshold => $np->threshold);
  1698. $np->add_perfdata(label => "io_device", value => $value6, uom => 'ms', threshold => $np->threshold);
  1699. $np->add_perfdata(label => "io_queue", value => $value7, uom => 'ms', threshold => $np->threshold);
  1700. $res = OK;
  1701. $output = "io commands aborted=" . $value1 . ", io bus resets=" . $value2 . ", io read latency=" . $value3 . " ms, write latency=" . $value4 . " ms, kernel latency=" . $value5 . " ms, device latency=" . $value6 . " ms, queue latency=" . $value7 ." ms";
  1702. }
  1703. }
  1704. return ($res, $output);
  1705. }
  1706. sub host_list_vm_volumes_info
  1707. {
  1708. my ($host, $np, $subcommand, $blacklist, $perc, $addopts) = @_;
  1709. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'datastore', 'runtime.inMaintenanceMode']);
  1710. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  1711. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  1712. die "Insufficient rights to access Datastores on the Host\n" if (!defined($host_view->datastore));
  1713. return datastore_volumes_info($host_view->datastore, $np, $subcommand, $blacklist, $perc, $addopts);
  1714. }
  1715. sub host_runtime_info
  1716. {
  1717. my ($host, $np, $subcommand, $blacklist, $addopts) = @_;
  1718. my $res = CRITICAL;
  1719. my $output = 'HOST RUNTIME Unknown error';
  1720. my $runtime;
  1721. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'runtime', 'overallStatus', 'configIssue']);
  1722. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  1723. $host_view->update_view_data(['name', 'runtime', 'overallStatus', 'configIssue']);
  1724. $runtime = $host_view->runtime;
  1725. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if ($runtime->inMaintenanceMode);
  1726. my %base_units = (
  1727. 'Degrees C' => 'C',
  1728. 'Degrees F' => 'F',
  1729. 'Degrees K' => 'K',
  1730. 'Volts' => 'V',
  1731. 'Amps' => 'A',
  1732. 'Watts' => 'W',
  1733. 'Percentage' => 'Pct'
  1734. );
  1735. my $blackregexpflag;
  1736. my $listitems;
  1737. $blackregexpflag = $addopts =~ m/(^|\s|\t|,)\Qblacklistregexp\E($|\s|\t|,)/ if (defined($addopts));
  1738. $listitems = $addopts =~ m/(^|\s|\t|,)\Qlistitems\E($|\s|\t|,)/ if (defined($addopts));
  1739. if (defined($subcommand))
  1740. {
  1741. if ($subcommand eq "CON")
  1742. {
  1743. $output = "connection state=" . $runtime->connectionState->val;
  1744. $res = OK if (uc($runtime->connectionState->val) eq "CONNECTED");
  1745. }
  1746. elsif ($subcommand eq "HEALTH")
  1747. {
  1748. my $OKCount = 0;
  1749. my $AlertCount = 0;
  1750. my ($cpuStatusInfo, $storageStatusInfo, $memoryStatusInfo, $numericSensorInfo);
  1751. my $components = {};
  1752. $res = UNKNOWN;
  1753. if(defined($runtime->healthSystemRuntime))
  1754. {
  1755. $cpuStatusInfo = $runtime->healthSystemRuntime->hardwareStatusInfo->cpuStatusInfo;
  1756. $storageStatusInfo = $runtime->healthSystemRuntime->hardwareStatusInfo->storageStatusInfo;
  1757. $memoryStatusInfo = $runtime->healthSystemRuntime->hardwareStatusInfo->memoryStatusInfo;
  1758. $numericSensorInfo = $runtime->healthSystemRuntime->systemHealthInfo->numericSensorInfo;
  1759. if (defined($cpuStatusInfo) || defined($storageStatusInfo) || defined($memoryStatusInfo) || defined($numericSensorInfo)) {
  1760. $res = OK;
  1761. }
  1762. $output = '';
  1763. if (defined($cpuStatusInfo))
  1764. {
  1765. foreach (@$cpuStatusInfo)
  1766. {
  1767. # print "CPU Name = ". $_->name .", Label = ". $_->status->label . ", Summary = ". $_->status->summary . ", Key = ". $_->status->key . "\n";
  1768. if (defined($blacklist))
  1769. {
  1770. my $name = $_->name;
  1771. next if ($blackregexpflag?$name =~ /$blacklist/:$blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  1772. }
  1773. my $state = check_health_state($_->status->key);
  1774. my $itemref = {
  1775. name => $_->name,
  1776. summary => $_->status->summary
  1777. };
  1778. push(@{$components->{$state}{CPU}}, $itemref);
  1779. $res = Nagios::Plugin::Functions::max_state_alt($res, $state);
  1780. if ($state != OK) {
  1781. $AlertCount++;
  1782. } else {
  1783. $OKCount++;
  1784. }
  1785. }
  1786. }
  1787. if (defined($storageStatusInfo))
  1788. {
  1789. foreach (@$storageStatusInfo)
  1790. {
  1791. # print "Storage Name = ". $_->name .", Label = ". $_->status->label . ", Summary = ". $_->status->summary . ", Key = ". $_->status->key . "\n";
  1792. if (defined($blacklist))
  1793. {
  1794. my $name = $_->name;
  1795. next if ($blackregexpflag?$name =~ /$blacklist/:$blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  1796. }
  1797. my $state = check_health_state($_->status->key);
  1798. my $itemref = {
  1799. name => $_->name,
  1800. summary => $_->status->summary
  1801. };
  1802. push(@{$components->{$state}{Storage}}, $itemref);
  1803. $res = Nagios::Plugin::Functions::max_state_alt($res, $state);
  1804. if ($state != OK) {
  1805. $AlertCount++;
  1806. } else {
  1807. $OKCount++;
  1808. }
  1809. }
  1810. }
  1811. if (defined($memoryStatusInfo))
  1812. {
  1813. foreach (@$memoryStatusInfo)
  1814. {
  1815. # print "Memory Name = ". $_->name .", Label = ". $_->status->label . ", Summary = ". $_->status->summary . ", Key = ". $_->status->key . "\n";
  1816. if (defined($blacklist))
  1817. {
  1818. my $name = $_->name;
  1819. next if ($blackregexpflag?$name =~ /$blacklist/:$blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  1820. }
  1821. my $state = check_health_state($_->status->key);
  1822. my $itemref = {
  1823. name => $_->name,
  1824. summary => $_->status->summary
  1825. };
  1826. push(@{$components->{$state}{Memory}}, $itemref);
  1827. $res = Nagios::Plugin::Functions::max_state_alt($res, $state);
  1828. if ($state != OK) {
  1829. $AlertCount++;
  1830. } else {
  1831. $OKCount++;
  1832. }
  1833. }
  1834. }
  1835. if (defined($numericSensorInfo))
  1836. {
  1837. foreach (@$numericSensorInfo)
  1838. {
  1839. # print "Sensor Name = ". $_->name .", Type = ". $_->sensorType . ", Label = ". $_->healthState->label . ", Summary = ". $_->healthState->summary . ", Key = " . $_->healthState->key . "\n";
  1840. if (defined($blacklist))
  1841. {
  1842. my $name = $_->name;
  1843. next if ($blackregexpflag?$name =~ /$blacklist/:$blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  1844. }
  1845. my $state = check_health_state($_->healthState->key);
  1846. my $itemref = {
  1847. name => $_->name,
  1848. summary => $_->healthState->summary
  1849. };
  1850. push(@{$components->{$state}{$_->sensorType}}, $itemref);
  1851. $res = Nagios::Plugin::Functions::max_state_alt($res, $state);
  1852. if ($state != OK) {
  1853. $AlertCount++;
  1854. } else {
  1855. $OKCount++;
  1856. }
  1857. }
  1858. }
  1859. if ($listitems) {
  1860. foreach my $fstate (reverse(sort(keys(%$components)))) {
  1861. foreach my $state_ref ($components->{$fstate}) {
  1862. foreach my $type (keys(%$state_ref)) {
  1863. foreach my $item_ref (@{$state_ref->{$type}}) {
  1864. $output .= "($STATUS_TEXT{$fstate}) \[$type\] | $item_ref->{name} | $item_ref->{summary}\n";
  1865. }
  1866. }
  1867. }
  1868. }
  1869. } else {
  1870. if ($AlertCount > 0) {
  1871. $output = "$AlertCount health issue(s) found in " . ($AlertCount + $OKCount) . " checks:\n";
  1872. my $AlertIndex = 0;
  1873. foreach my $fstate (reverse(sort(keys(%$components)))) {
  1874. next if ($fstate == 0);
  1875. foreach my $state_ref ( $components->{$fstate}) {
  1876. foreach my $type ( keys(%$state_ref)) {
  1877. foreach my $item_ref (@{$state_ref->{$type}}) {
  1878. $output .= ++$AlertIndex . ") $STATUS_TEXT{$fstate}\[$type\] Status of $item_ref->{name}: $item_ref->{summary}\n";
  1879. }
  1880. }
  1881. }
  1882. }
  1883. } else {
  1884. $output = "All $OKCount health checks are GREEN:";
  1885. foreach my $type (keys(%{$components->{0}})) {
  1886. $output .= " " . $type . " (" . (scalar(@{$components->{0}{$type}})) . "x);";
  1887. }
  1888. }
  1889. }
  1890. }
  1891. else
  1892. {
  1893. $output = "System health status unavailable";
  1894. }
  1895. $np->add_perfdata(label => "Alerts", value => $AlertCount);
  1896. }
  1897. elsif ($subcommand eq "STORAGEHEALTH")
  1898. {
  1899. my $OKCount = 0;
  1900. my $AlertCount = 0;
  1901. my $storageStatusInfo;
  1902. my $components = {};
  1903. $res = UNKNOWN;
  1904. if(defined($runtime->healthSystemRuntime) && defined($runtime->healthSystemRuntime->hardwareStatusInfo->storageStatusInfo))
  1905. {
  1906. $storageStatusInfo = $runtime->healthSystemRuntime->hardwareStatusInfo->storageStatusInfo;
  1907. $output = '';
  1908. foreach (@$storageStatusInfo)
  1909. {
  1910. if (defined($blacklist))
  1911. {
  1912. my $name = $_->name;
  1913. next if ($blackregexpflag?$name =~ /$blacklist/:$blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  1914. }
  1915. my $state = check_health_state($_->status->key);
  1916. $components->{$state}{"Storage"}{$_->name} = $_->status->summary;
  1917. if ($state != OK)
  1918. {
  1919. $res = Nagios::Plugin::Functions::max_state($res, $state);
  1920. $AlertCount++;
  1921. } else {
  1922. $OKCount++;
  1923. }
  1924. }
  1925. foreach my $fstate (reverse(sort(keys(%$components)))) {
  1926. foreach my $state_ref ($components->{$fstate}) {
  1927. foreach my $type (keys(%$state_ref)) {
  1928. foreach my $name (keys(%{$state_ref->{$type}})) {
  1929. $output .= "$STATUS_TEXT{$fstate}: Status of $name: $state_ref->{$type}{$name}\n";
  1930. }
  1931. }
  1932. }
  1933. }
  1934. if ($AlertCount > 0) {
  1935. $output = "$AlertCount health issue(s) found: \n" . $output;
  1936. } else {
  1937. $output = "All $OKCount Storage health checks are GREEN: \n" . $output;
  1938. $res = OK;
  1939. }
  1940. }
  1941. else
  1942. {
  1943. $res = UNKNOWN;
  1944. $output = "Storage health status unavailable";
  1945. }
  1946. $np->add_perfdata(label => "Alerts", value => $AlertCount);
  1947. }
  1948. elsif ($subcommand eq "TEMPERATURE")
  1949. {
  1950. my $OKCount = 0;
  1951. my $AlertCount = 0;
  1952. my $numericSensorInfo;
  1953. my $components = {};
  1954. $res = UNKNOWN;
  1955. if(defined($runtime->healthSystemRuntime))
  1956. {
  1957. $numericSensorInfo = $runtime->healthSystemRuntime->systemHealthInfo->numericSensorInfo;
  1958. $output = '';
  1959. if (defined($numericSensorInfo))
  1960. {
  1961. foreach (@$numericSensorInfo)
  1962. {
  1963. # print "Sensor Name = ". $_->name .", Type = ". $_->sensorType . ", Label = ". $_->healthState->label . ", Summary = ". $_->healthState->summary . ", Key = " . $_->healthState->key . "\n";
  1964. next if (uc($_->sensorType) ne 'TEMPERATURE');
  1965. if (defined($blacklist))
  1966. {
  1967. my $name = $_->name;
  1968. next if ($blackregexpflag?$name =~ /$blacklist/:$blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  1969. }
  1970. my $state = check_health_state($_->healthState->key);
  1971. $_->name =~ m/(.*?)\sTemp\s.+/;
  1972. my $itemref = {
  1973. name => $1,
  1974. power10 => $_->unitModifier,
  1975. state => $_->healthState->key,
  1976. value => $_->currentReading,
  1977. unit => $_->baseUnits,
  1978. };
  1979. push(@{$components->{$state}}, $itemref);
  1980. if ($state != OK)
  1981. {
  1982. $res = Nagios::Plugin::Functions::max_state($res, $state);
  1983. $AlertCount++;
  1984. }
  1985. else
  1986. {
  1987. $OKCount++;
  1988. }
  1989. if (exists($base_units{$itemref->{unit}}))
  1990. {
  1991. $np->add_perfdata(label => $itemref->{name}, value => ($itemref->{value} * 10 ** $itemref->{power10}), uom => $base_units{$itemref->{unit}});
  1992. }
  1993. else
  1994. {
  1995. $np->add_perfdata(label => $itemref->{name}, value => ($itemref->{value} * 10 ** $itemref->{power10}));
  1996. }
  1997. }
  1998. }
  1999. foreach my $curstate (reverse(sort(keys(%$components))))
  2000. {
  2001. foreach my $itemref (@{$components->{$curstate}})
  2002. {
  2003. my $value = $itemref->{value} * 10 ** $itemref->{power10};
  2004. my $unit = exists($base_units{$itemref->{unit}}) ? $base_units{$itemref->{unit}} : '';
  2005. my $name = $itemref->{name};
  2006. $output .= ", " if ($output);
  2007. $output .= "$STATUS_TEXT{$curstate} : $name = $value $unit";
  2008. }
  2009. }
  2010. if ($AlertCount > 0)
  2011. {
  2012. $output = "$AlertCount temperature issue(s) found:" . $output;
  2013. }
  2014. else
  2015. {
  2016. $output = "All $OKCount temperature checks are GREEN: " . $output;
  2017. $res = OK;
  2018. }
  2019. }
  2020. else
  2021. {
  2022. $output = "Temperature status unavailable";
  2023. }
  2024. }
  2025. elsif ($subcommand eq "SENSOR")
  2026. {
  2027. die "Provide sensor name as -o argument\n" if (!$addopts);
  2028. $output = '';
  2029. if(defined($runtime->healthSystemRuntime))
  2030. {
  2031. my $numericSensorInfo = $runtime->healthSystemRuntime->systemHealthInfo->numericSensorInfo;
  2032. if (defined($numericSensorInfo))
  2033. {
  2034. if ($addopts eq "listall")
  2035. {
  2036. foreach (@$numericSensorInfo)
  2037. {
  2038. $output .= "'" . $_->name . "', ";
  2039. }
  2040. if ($output)
  2041. {
  2042. chop($output);
  2043. chop($output);
  2044. $output = "numeric sensor list :" . $output;
  2045. }
  2046. else
  2047. {
  2048. $output = "numeric sensors unavailable";
  2049. }
  2050. }
  2051. else
  2052. {
  2053. foreach (@$numericSensorInfo)
  2054. {
  2055. if ($_->name =~ $addopts)
  2056. {
  2057. my $value = $_->currentReading * 10 ** $_->unitModifier;
  2058. $output = "sensor '" . $_->name . "' = " . $value . (defined($_->baseUnits) ? " " . $_->baseUnits : "");
  2059. $res = $np->check_threshold(check => $value);
  2060. $np->add_perfdata(label => $_->name, value => $value, threshold => $np->threshold);
  2061. last;
  2062. }
  2063. }
  2064. $output = "Can not find sensor by name '" . $addopts . "'" if (!$output);
  2065. }
  2066. }
  2067. else
  2068. {
  2069. $res = UNKNOWN;
  2070. $output = "System numeric sensors status unavailable";
  2071. }
  2072. }
  2073. else
  2074. {
  2075. $res = UNKNOWN;
  2076. $output = "System health status unavailable";
  2077. }
  2078. }
  2079. elsif ($subcommand eq "MAINTENANCE")
  2080. {
  2081. my %host_maintenance_state = (0 => "no", 1 => "yes");
  2082. $output = "maintenance=" . $host_maintenance_state{$runtime->inMaintenanceMode};
  2083. $res = OK;
  2084. }
  2085. elsif (($subcommand eq "LIST") || ($subcommand eq "LISTVM"))
  2086. {
  2087. my %vm_state_strings = ("poweredOn" => "UP", "poweredOff" => "DOWN", "suspended" => "SUSPENDED");
  2088. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $host_view, properties => ['name', 'runtime']);
  2089. die "Runtime error\n" if (!defined($vm_views));
  2090. die "There are no VMs.\n" if (!@$vm_views);
  2091. my $up = 0;
  2092. $output = '';
  2093. foreach my $vm (@$vm_views)
  2094. {
  2095. my $vm_state = $vm_state_strings{$vm->runtime->powerState->val};
  2096. if ($vm_state eq "UP")
  2097. {
  2098. $up++;
  2099. $output .= $vm->name . "(OK), ";
  2100. }
  2101. else
  2102. {
  2103. $output = $vm->name . "(" . $vm_state . "), " . $output;
  2104. }
  2105. }
  2106. chop($output);
  2107. chop($output);
  2108. $res = OK;
  2109. $output = $up . "/" . @$vm_views . " VMs up: " . $output;
  2110. $np->add_perfdata(label => "vmcount", value => $up, uom => 'units', threshold => $np->threshold);
  2111. $res = $np->check_threshold(check => $up) if (defined($np->threshold));
  2112. }
  2113. elsif ($subcommand eq "STATUS")
  2114. {
  2115. my $status = $host_view->overallStatus->val;
  2116. $output = "overall status=" . $status;
  2117. $res = check_health_state($status);
  2118. }
  2119. elsif ($subcommand eq "ISSUES")
  2120. {
  2121. my $issues = $host_view->configIssue;
  2122. $output = '';
  2123. if (defined($issues))
  2124. {
  2125. foreach (@$issues)
  2126. {
  2127. if (defined($blacklist))
  2128. {
  2129. my $name = ref($_);
  2130. next if ($blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  2131. }
  2132. $output .= format_issue($_) . "; ";
  2133. }
  2134. }
  2135. if ($output eq '')
  2136. {
  2137. $res = OK;
  2138. $output = 'No config issues';
  2139. }
  2140. }
  2141. else
  2142. {
  2143. $res = CRITICAL;
  2144. $output = "HOST RUNTIME - unknown subcommand\n" . $np->opts->_help;
  2145. }
  2146. }
  2147. else
  2148. {
  2149. my %host_maintenance_state = (0 => "no", 1 => "yes");
  2150. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $host_view, properties => ['name', 'runtime']);
  2151. my $up = 0;
  2152. die "Runtime error\n" if (!defined($vm_views));
  2153. if (@$vm_views)
  2154. {
  2155. foreach my $vm (@$vm_views)
  2156. {
  2157. $up += $vm->runtime->powerState->val eq "poweredOn";
  2158. }
  2159. $output = $up . "/" . @$vm_views . " VMs up";
  2160. }
  2161. else
  2162. {
  2163. $output = "No VMs installed";
  2164. }
  2165. $np->add_perfdata(label => "vmcount", value => $up, uom => 'units', threshold => $np->threshold);
  2166. my $AlertCount = 0;
  2167. my $SensorCount = 0;
  2168. my ($cpuStatusInfo, $storageStatusInfo, $memoryStatusInfo, $numericSensorInfo);
  2169. if(defined($runtime->healthSystemRuntime))
  2170. {
  2171. $cpuStatusInfo = $runtime->healthSystemRuntime->hardwareStatusInfo->cpuStatusInfo;
  2172. $storageStatusInfo = $runtime->healthSystemRuntime->hardwareStatusInfo->storageStatusInfo;
  2173. $memoryStatusInfo = $runtime->healthSystemRuntime->hardwareStatusInfo->memoryStatusInfo;
  2174. $numericSensorInfo = $runtime->healthSystemRuntime->systemHealthInfo->numericSensorInfo;
  2175. }
  2176. if (defined($cpuStatusInfo))
  2177. {
  2178. foreach (@$cpuStatusInfo)
  2179. {
  2180. $SensorCount++;
  2181. $AlertCount++ if (check_health_state($_->status->key) != OK);
  2182. }
  2183. }
  2184. if (defined($storageStatusInfo))
  2185. {
  2186. foreach (@$storageStatusInfo)
  2187. {
  2188. $SensorCount++;
  2189. $AlertCount++ if (check_health_state($_->status->key) != OK);
  2190. }
  2191. }
  2192. if (defined($memoryStatusInfo))
  2193. {
  2194. foreach (@$memoryStatusInfo)
  2195. {
  2196. $SensorCount++;
  2197. $AlertCount++ if (check_health_state($_->status->key) != OK);
  2198. }
  2199. }
  2200. if (defined($numericSensorInfo))
  2201. {
  2202. foreach (@$numericSensorInfo)
  2203. {
  2204. $SensorCount++;
  2205. $AlertCount++ if (check_health_state($_->healthState->key) != OK);
  2206. }
  2207. }
  2208. $res = OK;
  2209. $output .= ", overall status=" . $host_view->overallStatus->val . ", connection state=" . $runtime->connectionState->val . ", maintenance=" . $host_maintenance_state{$runtime->inMaintenanceMode} . ", ";
  2210. if ($AlertCount)
  2211. {
  2212. $output .= "$AlertCount health issue(s), ";
  2213. }
  2214. else
  2215. {
  2216. $output .= "All $SensorCount health checks are Green, ";
  2217. }
  2218. $np->add_perfdata(label => "health_issues", value => $AlertCount);
  2219. my $issues = $host_view->configIssue;
  2220. if (defined($issues))
  2221. {
  2222. $output .= @$issues . " config issue(s)";
  2223. $np->add_perfdata(label => "config_issues", value => "" . @$issues);
  2224. }
  2225. else
  2226. {
  2227. $output .= "no config issues";
  2228. $np->add_perfdata(label => "config_issues", value => 0);
  2229. }
  2230. }
  2231. return ($res, $output);
  2232. }
  2233. sub host_service_info
  2234. {
  2235. my ($host, $np, $subcommand) = @_;
  2236. my $res = CRITICAL;
  2237. my $output = 'HOST RUNTIME Unknown error';
  2238. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'configManager', 'runtime.inMaintenanceMode']);
  2239. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  2240. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  2241. my $services = Vim::get_view(mo_ref => $host_view->configManager->serviceSystem, properties => ['serviceInfo'])->serviceInfo->service;
  2242. if (defined($subcommand))
  2243. {
  2244. $subcommand = ',' . $subcommand . ',';
  2245. $output = '';
  2246. foreach (@$services)
  2247. {
  2248. my $srvname = $_->key;
  2249. if ($subcommand =~ s/,$srvname,/,/g)
  2250. {
  2251. while ($subcommand =~ s/,$srvname,/,/g){};
  2252. $output .= $srvname . ", " if (!$_->running);
  2253. }
  2254. }
  2255. $subcommand =~ s/^,//;
  2256. chop($subcommand);
  2257. if ($subcommand ne '')
  2258. {
  2259. $res = UNKNOWN;
  2260. $output = "unknown services : $subcommand";
  2261. }
  2262. elsif ($output eq '')
  2263. {
  2264. $res = OK;
  2265. $output = "All services are in their apropriate state.";
  2266. }
  2267. else
  2268. {
  2269. chop($output);
  2270. chop($output);
  2271. $output .= " are down";
  2272. }
  2273. }
  2274. else
  2275. {
  2276. my %service_state = (0 => "down", 1 => "up");
  2277. $res = OK;
  2278. $output = "services : ";
  2279. $output .= $_->key . " (" . $service_state{$_->running} . "), " foreach (@$services);
  2280. chop($output);
  2281. chop($output);
  2282. }
  2283. return ($res, $output);
  2284. }
  2285. sub host_storage_info
  2286. {
  2287. my ($host, $np, $subcommand, $blacklist) = @_;
  2288. my $count = 0;
  2289. my $res = CRITICAL;
  2290. my $output = 'HOST RUNTIME Unknown error';
  2291. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'configManager', 'runtime.inMaintenanceMode']);
  2292. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  2293. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  2294. my $storage = Vim::get_view(mo_ref => $host_view->configManager->storageSystem, properties => ['storageDeviceInfo']);
  2295. if (defined($subcommand))
  2296. {
  2297. if ($subcommand eq "ADAPTER")
  2298. {
  2299. $output = "";
  2300. $res = OK;
  2301. foreach my $dev (@{$storage->storageDeviceInfo->hostBusAdapter})
  2302. {
  2303. my $name = $dev->device;
  2304. my $status = $dev->status;
  2305. if (defined($blacklist))
  2306. {
  2307. my $key = $dev->key;
  2308. if (($blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/) || ($blacklist =~ m/(^|\s|\t|,)\Q$key\E($|\s|\t|,)/))
  2309. {
  2310. $count++;
  2311. $status = "ignored";
  2312. }
  2313. }
  2314. $count ++ if (uc($status) eq "ONLINE");
  2315. $res = UNKNOWN if (uc($status) eq "UNKNOWN");
  2316. $output .= $name . " (" . $status . "); ";
  2317. }
  2318. my $state = $np->check_threshold(check => $count);
  2319. $res = $state if ($state != OK);
  2320. $np->add_perfdata(label => "adapters", value => $count, uom => 'units', threshold => $np->threshold);
  2321. }
  2322. elsif ($subcommand eq "LUN")
  2323. {
  2324. $output = "";
  2325. $res = OK;
  2326. my $state = OK; # For unkonwn or other statuses
  2327. foreach my $scsi (@{$storage->storageDeviceInfo->scsiLun})
  2328. {
  2329. my $name = "";
  2330. if (exists($scsi->{displayName}))
  2331. {
  2332. $name = $scsi->displayName;
  2333. }
  2334. elsif (exists($scsi->{canonicalName}))
  2335. {
  2336. $name = $scsi->canonicalName;
  2337. }
  2338. else
  2339. {
  2340. $name = $scsi->deviceName;
  2341. }
  2342. $state = OK;
  2343. my $operationState;
  2344. if (defined($blacklist) && ($blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/))
  2345. {
  2346. $operationState = "ignored";
  2347. }
  2348. else
  2349. {
  2350. $operationState = join("-", @{$scsi->operationalState});
  2351. foreach (@{$scsi->operationalState})
  2352. {
  2353. if (uc($_) eq "OK")
  2354. {
  2355. # $state = OK;
  2356. }
  2357. elsif (uc($_) eq "UNKNOWN")
  2358. {
  2359. $res = UNKNOWN;
  2360. }
  2361. elsif (uc($_) eq "UNKNOWNSTATE")
  2362. {
  2363. $res = UNKNOWN;
  2364. }
  2365. else
  2366. {
  2367. $state = CRITICAL;
  2368. }
  2369. }
  2370. }
  2371. $count++ if ($state == OK);
  2372. $output .= $name . " <" . $operationState . ">; ";
  2373. }
  2374. $np->add_perfdata(label => "LUNs", value => $count, uom => 'units', threshold => $np->threshold);
  2375. $state = $np->check_threshold(check => $count);
  2376. $res = $state if ($state != OK);
  2377. }
  2378. elsif ($subcommand eq "PATH")
  2379. {
  2380. if (exists($storage->storageDeviceInfo->{multipathInfo}))
  2381. {
  2382. $output = "";
  2383. $res = OK;
  2384. foreach my $lun (@{$storage->storageDeviceInfo->multipathInfo->lun})
  2385. {
  2386. foreach my $path (@{$lun->path})
  2387. {
  2388. my $status = UNKNOWN; # For unknown or other statuses
  2389. my $pathState = "unknown";
  2390. my $name = $path->name;
  2391. if (exists($path->{state}))
  2392. {
  2393. $pathState = $path->state;
  2394. }
  2395. else
  2396. {
  2397. $pathState = $path->pathState;
  2398. }
  2399. if (defined($blacklist) && ($blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/))
  2400. {
  2401. $pathState = "ignored";
  2402. $count++;
  2403. }
  2404. my $normalizedPathState = uc($pathState);
  2405. $count++ if ($normalizedPathState eq "ACTIVE");
  2406. $res = UNKNOWN if (($res == OK) && ($normalizedPathState eq "UNKNOWN"));
  2407. $res = CRITICAL if ($normalizedPathState eq "DEAD");
  2408. $output .= $name . " <" . $pathState . ">; ";
  2409. }
  2410. }
  2411. $np->add_perfdata(label => "paths", value => $count, uom => 'units', threshold => $np->threshold);
  2412. my $state = $np->check_threshold(check => $count);
  2413. $res = $state if ($state != OK);
  2414. }
  2415. else
  2416. {
  2417. $output = "path info is unavailable on this host";
  2418. $res = UNKNOWN;
  2419. }
  2420. }
  2421. else
  2422. {
  2423. $res = CRITICAL;
  2424. $output = "HOST STORAGE - unknown subcommand\n" . $np->opts->_help;
  2425. }
  2426. }
  2427. else
  2428. {
  2429. my $status = UNKNOWN;
  2430. my $state = OK;
  2431. $output = "";
  2432. $res = OK;
  2433. foreach my $dev (@{$storage->storageDeviceInfo->hostBusAdapter})
  2434. {
  2435. $status = UNKNOWN;
  2436. if (uc($dev->status) eq "ONLINE")
  2437. {
  2438. $status = OK;
  2439. $count++;
  2440. }
  2441. elsif (uc($dev->status) eq "OFFLINE")
  2442. {
  2443. $status = CRITICAL;
  2444. }
  2445. elsif (uc($dev->status) eq "FAULT")
  2446. {
  2447. $status = CRITICAL;
  2448. }
  2449. else
  2450. {
  2451. $res = UNKNOWN;
  2452. }
  2453. $state = Nagios::Plugin::Functions::max_state($state, $status);
  2454. }
  2455. $np->add_perfdata(label => "adapters", value => $count, uom => 'units', threshold => $np->threshold);
  2456. $output .= $count . "/" . @{$storage->storageDeviceInfo->hostBusAdapter} . " adapters online, ";
  2457. $count = 0;
  2458. foreach my $scsi (@{$storage->storageDeviceInfo->scsiLun})
  2459. {
  2460. $status = UNKNOWN;
  2461. foreach (@{$scsi->operationalState})
  2462. {
  2463. if (uc($_) eq "OK")
  2464. {
  2465. $status = OK;
  2466. $count++;
  2467. }
  2468. elsif (uc($_) eq "ERROR")
  2469. {
  2470. $status = CRITICAL;
  2471. }
  2472. elsif (uc($_) eq "UNKNOWNSTATE")
  2473. {
  2474. $status = UNKNOWN;
  2475. }
  2476. elsif (uc($_) eq "OFF")
  2477. {
  2478. $status = CRITICAL;
  2479. }
  2480. elsif (uc($_) eq "QUIESCED")
  2481. {
  2482. $status = WARNING;
  2483. }
  2484. elsif (uc($_) eq "DEGRADED")
  2485. {
  2486. $status = WARNING;
  2487. }
  2488. elsif (uc($_) eq "LOSTCOMMUNICATION")
  2489. {
  2490. $status = CRITICAL;
  2491. }
  2492. else
  2493. {
  2494. $res = UNKNOWN;
  2495. $status = UNKNOWN;
  2496. }
  2497. $state = Nagios::Plugin::Functions::max_state($state, $status);
  2498. }
  2499. }
  2500. $np->add_perfdata(label => "LUNs", value => $count, uom => 'units', threshold => $np->threshold);
  2501. $output .= $count . "/" . @{$storage->storageDeviceInfo->scsiLun} . " LUNs ok, ";
  2502. if (exists($storage->storageDeviceInfo->{multipathInfo}))
  2503. {
  2504. $count = 0;
  2505. my $amount = 0;
  2506. foreach my $lun (@{$storage->storageDeviceInfo->multipathInfo->lun})
  2507. {
  2508. foreach my $path (@{$lun->path})
  2509. {
  2510. my $status = UNKNOWN; # For unkonwn or other statuses
  2511. my $pathState = "unknown";
  2512. if (exists($path->{state}))
  2513. {
  2514. $pathState = $path->state;
  2515. }
  2516. else
  2517. {
  2518. $pathState = $path->pathState;
  2519. }
  2520. $status = UNKNOWN;
  2521. if (uc($pathState) eq "ACTIVE")
  2522. {
  2523. $status = OK;
  2524. $count++;
  2525. }
  2526. elsif (uc($pathState) eq "DISABLED")
  2527. {
  2528. $status = WARNING;
  2529. }
  2530. elsif (uc($pathState) eq "STANDBY")
  2531. {
  2532. $status = WARNING;
  2533. }
  2534. elsif (uc($pathState) eq "DEAD")
  2535. {
  2536. $status = CRITICAL;
  2537. }
  2538. else
  2539. {
  2540. $res = UNKNOWN;
  2541. $status = UNKNOWN;
  2542. }
  2543. $state = Nagios::Plugin::Functions::max_state($state, $status);
  2544. $amount++;
  2545. }
  2546. }
  2547. $np->add_perfdata(label => "paths", value => $count, uom => 'units', threshold => $np->threshold);
  2548. $output .= $count . "/" . $amount . " paths active";
  2549. }
  2550. else
  2551. {
  2552. $output .= "no path info";
  2553. }
  2554. $res = $state if ($state != OK);
  2555. }
  2556. return ($res, $output);
  2557. }
  2558. sub format_uptime {
  2559. my($uptime) = @_;
  2560. my $days = $uptime / (60 * 60 * 24);
  2561. $uptime %= (60 * 60 * 24);
  2562. my $hours = $uptime / (60 * 60);
  2563. $uptime %= (60 * 60);
  2564. my $minutes = $uptime / 60;
  2565. my $seconds = $uptime % 60;
  2566. if ($days == 0)
  2567. {
  2568. $output = sprintf("%d:%02d:%02d", $hours, $minutes, $seconds);
  2569. }
  2570. elsif ($days == 1)
  2571. {
  2572. $output = sprintf("1 day, %d:%02d:%02d", $hours, $minutes, $seconds);
  2573. }
  2574. else {
  2575. $output = sprintf("%d days, %d:%02d:%02d", $days, $hours, $minutes, $seconds);
  2576. }
  2577. return $output;
  2578. }
  2579. sub host_uptime_info
  2580. {
  2581. my ($host, $np, $addopts) = @_;
  2582. my $res = CRITICAL;
  2583. my $output = 'HOST UPTIME Unknown error';
  2584. my $quickStats;
  2585. $quickStats = $addopts =~ m/(^|\s|\t|,)\Qquickstats\E($|\s|\t|,)/ if (defined($addopts));
  2586. my $value;
  2587. if (defined($quickStats))
  2588. {
  2589. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'runtime.inMaintenanceMode', 'summary.quickStats']);
  2590. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  2591. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if (uc($host_view->get_property('runtime.inMaintenanceMode')) eq "TRUE");
  2592. $values = $host_view->get_property('summary.quickStats');
  2593. $value = simplify_number($values->uptime, 0) if exists($values->{uptime});
  2594. }
  2595. else
  2596. {
  2597. $values = return_host_performance_values($host, $defperfargs, 'sys', ('uptime.latest'));
  2598. $value = simplify_number(convert_number($$values[0][0]->value), 0) if (defined($values));
  2599. }
  2600. if (defined($value))
  2601. {
  2602. $np->add_perfdata(label => "uptime", value => $value, uom => 's', threshold => $np->threshold);
  2603. $output = "uptime=" . format_uptime($value);
  2604. $res = $np->check_threshold(check => $value);
  2605. }
  2606. return ($res, $output);
  2607. }
  2608. sub host_device_info
  2609. {
  2610. my ($host, $np, $subcommand, $addopts) = @_;
  2611. my $count = 0;
  2612. my $res = CRITICAL;
  2613. my $output = 'HOST DEVICE Unknown error';
  2614. if (defined($subcommand) && ($subcommand eq 'cd/dvd')) {
  2615. my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => $host, properties => ['name', 'runtime']);
  2616. die "Host \"" . $$host{"name"} . "\" does not exist\n" if (!defined($host_view));
  2617. die {msg => ("NOTICE: \"" . $host_view->name . "\" is in maintenance mode, check skipped\n"), code => OK} if ($host_view->runtime->inMaintenanceMode);
  2618. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $host_view, properties => ['name', 'config.template', 'config.hardware.device', 'runtime.powerState']);
  2619. die "Runtime error\n" if (!defined($vm_views));
  2620. my $listall;
  2621. $listall = $addopts =~ m/(^|\s|\t|,)\Qlistall\E($|\s|\t|,)/ if (defined($addopts));
  2622. $output = '';
  2623. foreach my $vm (@$vm_views)
  2624. {
  2625. # change get_property to {} to avoid infinite loop
  2626. my $istemplate = $vm->{'config.template'};
  2627. next if ($istemplate && ($istemplate eq 'true'));
  2628. my $match = 0;
  2629. my $displayname = $vm->name;
  2630. my $devices = $vm->{'config.hardware.device'};
  2631. if ($subcommand eq 'cd/dvd') {
  2632. foreach my $dev (@$devices) {
  2633. $match++ if ((ref($dev) eq "VirtualCdrom") && ($dev->connectable->connected == 1));
  2634. }
  2635. }
  2636. if ($match) {
  2637. $count++;
  2638. $output = "\"$displayname\"($match), " . $output;
  2639. } else {
  2640. $output .= "\"$displayname\"($match), " if ($listall);
  2641. }
  2642. }
  2643. if ($output ne '') {
  2644. chop($output);
  2645. chop($output);
  2646. }
  2647. if ($count) {
  2648. $output = "VM's with $subcommand devices: " . $output;
  2649. } else {
  2650. $output = $listall ? "VM's without $subcommand devices: " . $output : "No VM's with $subcommand devices";
  2651. }
  2652. $np->add_perfdata(label => "match", value => $count, threshold => $np->threshold);
  2653. $res = $np->check_threshold(check => $count);
  2654. } else {
  2655. $res = CRITICAL;
  2656. $output = "HOST DEVICE - unknown subcommand\n" . $np->opts->_help;
  2657. }
  2658. return ($res, $output);
  2659. }
  2660. #==========================================================================| VM |============================================================================#
  2661. sub vm_cpu_info
  2662. {
  2663. my ($vmname, $np, $subcommand) = @_;
  2664. my $res = CRITICAL;
  2665. my $output = 'HOST-VM CPU Unknown error';
  2666. if (defined($subcommand))
  2667. {
  2668. if ($subcommand eq "USAGE")
  2669. {
  2670. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'cpu', ('usage.average'));
  2671. if (defined($values))
  2672. {
  2673. my $value = simplify_number(convert_number($$values[0][0]->value) * 0.01);
  2674. $np->add_perfdata(label => "cpu_usage", value => $value, uom => '%', threshold => $np->threshold);
  2675. $output = "\"$vmname\" cpu usage=" . $value . " %";
  2676. $res = $np->check_threshold(check => $value);
  2677. }
  2678. }
  2679. elsif ($subcommand eq "USAGEMHZ")
  2680. {
  2681. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'cpu', ('usagemhz.average'));
  2682. if (defined($values))
  2683. {
  2684. my $value = simplify_number(convert_number($$values[0][0]->value));
  2685. $np->add_perfdata(label => "cpu_usagemhz", value => $value, uom => 'MHz', threshold => $np->threshold);
  2686. $output = "\"$vmname\" cpu usage=" . $value . " MHz";
  2687. $res = $np->check_threshold(check => $value);
  2688. }
  2689. }
  2690. elsif ($subcommand eq "WAIT")
  2691. {
  2692. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'cpu', ('wait.summation:*'));
  2693. if (defined($values))
  2694. {
  2695. my $value = simplify_number(convert_number($$values[0][0]->value));
  2696. $np->add_perfdata(label => "cpu_wait", value => $value, uom => 'ms', threshold => $np->threshold);
  2697. $output = "\"$vmname\" cpu wait=" . $value . " ms";
  2698. $res = $np->check_threshold(check => $value);
  2699. }
  2700. }
  2701. elsif ($subcommand eq "READY")
  2702. {
  2703. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'cpu', ('ready.summation:*'));
  2704. if (defined($values))
  2705. {
  2706. my $value = simplify_number(convert_number($$values[0][0]->value));
  2707. $np->add_perfdata(label => "cpu_ready", value => $value, uom => 'ms', threshold => $np->threshold);
  2708. $output = "\"$vmname\" cpu ready=" . $value . " ms";
  2709. $res = $np->check_threshold(check => $value);
  2710. }
  2711. }
  2712. else
  2713. {
  2714. $res = CRITICAL;
  2715. $output = "HOST-VM CPU - unknown subcommand\n" . $np->opts->_help;
  2716. }
  2717. }
  2718. else
  2719. {
  2720. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'cpu', ('usagemhz.average', 'usage.average', 'wait.summation:*', 'ready.summation:*'));
  2721. if (defined($values))
  2722. {
  2723. my $value1 = simplify_number(convert_number($$values[0][0]->value));
  2724. my $value2 = simplify_number(convert_number($$values[0][1]->value) * 0.01);
  2725. my $value3 = simplify_number(convert_number($$values[0][2]->value));
  2726. my $value4 = simplify_number(convert_number($$values[0][3]->value));
  2727. $np->add_perfdata(label => "cpu_usagemhz", value => $value1, uom => 'MHz', threshold => $np->threshold);
  2728. $np->add_perfdata(label => "cpu_usage", value => $value2, uom => '%', threshold => $np->threshold);
  2729. $np->add_perfdata(label => "cpu_wait", value => $value3, uom => 'ms', threshold => $np->threshold);
  2730. $np->add_perfdata(label => "cpu_ready", value => $value4, uom => 'ms', threshold => $np->threshold);
  2731. $res = OK;
  2732. $output = "\"$vmname\" cpu usage=" . $value1 . " MHz(" . $value2 . "%), wait=" . $value3 . " ms, ready=" . $value4 . " ms";
  2733. }
  2734. }
  2735. return ($res, $output);
  2736. }
  2737. sub vm_mem_info
  2738. {
  2739. my ($vmname, $np, $subcommand) = @_;
  2740. my $res = CRITICAL;
  2741. my $output = 'HOST-VM MEM Unknown error';
  2742. if (defined($subcommand))
  2743. {
  2744. if ($subcommand eq "USAGE")
  2745. {
  2746. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('usage.average'));
  2747. if (defined($values))
  2748. {
  2749. my $value = simplify_number(convert_number($$values[0][0]->value) * 0.01);
  2750. $np->add_perfdata(label => "mem_usage", value => $value, uom => '%', threshold => $np->threshold);
  2751. $output = "\"$vmname\" mem usage=" . $value . " %";
  2752. $res = $np->check_threshold(check => $value);
  2753. }
  2754. }
  2755. elsif ($subcommand eq "USAGEMB")
  2756. {
  2757. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('consumed.average'));
  2758. if (defined($values))
  2759. {
  2760. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2761. $np->add_perfdata(label => "mem_usagemb", value => $value, uom => 'MB', threshold => $np->threshold);
  2762. $output = "\"$vmname\" mem usage=" . $value . " MB";
  2763. $res = $np->check_threshold(check => $value);
  2764. }
  2765. }
  2766. elsif ($subcommand eq "SWAP")
  2767. {
  2768. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('swapped.average'));
  2769. if (defined($values))
  2770. {
  2771. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2772. $np->add_perfdata(label => "mem_swap", value => $value, uom => 'MB', threshold => $np->threshold);
  2773. $output = "\"$vmname\" swap usage=" . $value . " MB";
  2774. $res = $np->check_threshold(check => $value);
  2775. }
  2776. }
  2777. elsif ($subcommand eq "SWAPIN")
  2778. {
  2779. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('swapin.average'));
  2780. if (defined($values))
  2781. {
  2782. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2783. $np->add_perfdata(label => "mem_swapin", value => $value, uom => 'MB', threshold => $np->threshold);
  2784. $output = "\"$vmname\" swapin=" . $value . " MB";
  2785. $res = $np->check_threshold(check => $value);
  2786. }
  2787. }
  2788. elsif ($subcommand eq "SWAPOUT")
  2789. {
  2790. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('swapout.average'));
  2791. if (defined($values))
  2792. {
  2793. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2794. $np->add_perfdata(label => "mem_swapout", value => $value, uom => 'MB', threshold => $np->threshold);
  2795. $output = "\"$vmname\" swapout=" . $value . " MB";
  2796. $res = $np->check_threshold(check => $value);
  2797. }
  2798. }
  2799. elsif ($subcommand eq "OVERHEAD")
  2800. {
  2801. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('overhead.average'));
  2802. if (defined($values))
  2803. {
  2804. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2805. $np->add_perfdata(label => "mem_overhead", value => $value, uom => 'MB', threshold => $np->threshold);
  2806. $output = "\"$vmname\" mem overhead=" . $value . " MB";
  2807. $res = $np->check_threshold(check => $value);
  2808. }
  2809. }
  2810. elsif ($subcommand eq "OVERALL")
  2811. {
  2812. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('consumed.average', 'overhead.average'));
  2813. if (defined($values))
  2814. {
  2815. my $value = simplify_number((convert_number($$values[0][0]->value) + convert_number($$values[0][1]->value)) / 1024);
  2816. $np->add_perfdata(label => "mem_overall", value => $value, uom => 'MB', threshold => $np->threshold);
  2817. $output = "\"$vmname\" mem overall=" . $value . " MB";
  2818. $res = $np->check_threshold(check => $value);
  2819. }
  2820. }
  2821. elsif ($subcommand eq "ACTIVE")
  2822. {
  2823. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('active.average'));
  2824. if (defined($values))
  2825. {
  2826. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2827. $np->add_perfdata(label => "mem_active", value => $value, uom => 'MB', threshold => $np->threshold);
  2828. $output = "\"$vmname\" mem active=" . $value . " MB";
  2829. $res = $np->check_threshold(check => $value);
  2830. }
  2831. }
  2832. elsif ($subcommand eq "MEMCTL")
  2833. {
  2834. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('vmmemctl.average'));
  2835. if (defined($values))
  2836. {
  2837. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2838. $np->add_perfdata(label => "mem_memctl", value => $value, uom => 'MB', threshold => $np->threshold);
  2839. $output = "\"$vmname\" mem memctl=" . $value . " MB";
  2840. $res = $np->check_threshold(check => $value);
  2841. }
  2842. }
  2843. else
  2844. {
  2845. $res = CRITICAL;
  2846. $output = "HOST-VM MEM - unknown subcommand\n" . $np->opts->_help;
  2847. }
  2848. }
  2849. else
  2850. {
  2851. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'mem', ('consumed.average', 'usage.average', 'overhead.average', 'active.average', 'swapped.average', 'swapin.average', 'swapout.average', 'vmmemctl.average'));
  2852. if (defined($values))
  2853. {
  2854. my $value1 = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2855. my $value2 = simplify_number(convert_number($$values[0][1]->value) * 0.01);
  2856. my $value3 = simplify_number(convert_number($$values[0][2]->value) / 1024);
  2857. my $value4 = simplify_number(convert_number($$values[0][3]->value) / 1024);
  2858. my $value5 = simplify_number(convert_number($$values[0][4]->value) / 1024);
  2859. my $value6 = simplify_number(convert_number($$values[0][5]->value) / 1024);
  2860. my $value7 = simplify_number(convert_number($$values[0][6]->value) / 1024);
  2861. my $value8 = simplify_number(convert_number($$values[0][7]->value) / 1024);
  2862. $np->add_perfdata(label => "mem_usagemb", value => $value1, uom => 'MB', threshold => $np->threshold);
  2863. $np->add_perfdata(label => "mem_usage", value => $value2, uom => '%', threshold => $np->threshold);
  2864. $np->add_perfdata(label => "mem_overhead", value => $value3, uom => 'MB', threshold => $np->threshold);
  2865. $np->add_perfdata(label => "mem_active", value => $value4, uom => 'MB', threshold => $np->threshold);
  2866. $np->add_perfdata(label => "mem_swap", value => $value5, uom => 'MB', threshold => $np->threshold);
  2867. $np->add_perfdata(label => "mem_swapin", value => $value6, uom => 'MB', threshold => $np->threshold);
  2868. $np->add_perfdata(label => "mem_swapout", value => $value7, uom => 'MB', threshold => $np->threshold);
  2869. $np->add_perfdata(label => "mem_memctl", value => $value8, uom => 'MB', threshold => $np->threshold);
  2870. $res = OK;
  2871. $output = "\"$vmname\" mem usage=" . $value1 . " MB(" . $value2 . "%), overhead=" . $value3 . " MB, active=" . $value4 . " MB, swapped=" . $value5 . " MB, swapin=" . $value6 . " MB, swapout=" . $value7 . " MB, memctl=" . $value8 . " MB";
  2872. }
  2873. }
  2874. return ($res, $output);
  2875. }
  2876. sub vm_net_info
  2877. {
  2878. my ($vmname, $np, $subcommand) = @_;
  2879. my $res = CRITICAL;
  2880. my $output = 'HOST-VM NET Unknown error';
  2881. if (defined($subcommand))
  2882. {
  2883. if ($subcommand eq "USAGE")
  2884. {
  2885. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'net', ('usage.average:*'));
  2886. if (defined($values))
  2887. {
  2888. my $value = simplify_number(convert_number($$values[0][0]->value));
  2889. $np->add_perfdata(label => "net_usage", value => $value, uom => 'KBps', threshold => $np->threshold);
  2890. $output = "\"$vmname\" net usage=" . $value . " KBps";
  2891. $res = $np->check_threshold(check => $value);
  2892. }
  2893. }
  2894. elsif ($subcommand eq "RECEIVE")
  2895. {
  2896. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'net', ('received.average:*'));
  2897. if (defined($values))
  2898. {
  2899. my $value = simplify_number(convert_number($$values[0][0]->value));
  2900. $np->add_perfdata(label => "net_receive", value => $value, uom => 'KBps', threshold => $np->threshold);
  2901. $output = "\"$vmname\" net receive=" . $value . " KBps";
  2902. $res = $np->check_threshold(check => $value);
  2903. }
  2904. }
  2905. elsif ($subcommand eq "SEND")
  2906. {
  2907. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'net', ('transmitted.average:*'));
  2908. if (defined($values))
  2909. {
  2910. my $value = simplify_number(convert_number($$values[0][0]->value));
  2911. $np->add_perfdata(label => "net_send", value => $value, uom => 'KBps', threshold => $np->threshold);
  2912. $output = "\"$vmname\" net send=" . $value . " KBps";
  2913. $res = $np->check_threshold(check => $value);
  2914. }
  2915. }
  2916. else
  2917. {
  2918. $res = CRITICAL;
  2919. $output = "HOST-VM NET - unknown subcommand\n" . $np->opts->_help;
  2920. }
  2921. }
  2922. else
  2923. {
  2924. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'net', ('received.average:*', 'transmitted.average:*'));
  2925. if (defined($values))
  2926. {
  2927. my $value1 = simplify_number(convert_number($$values[0][0]->value));
  2928. my $value2 = simplify_number(convert_number($$values[0][1]->value));
  2929. $np->add_perfdata(label => "net_receive", value => $value1, uom => 'KBps', threshold => $np->threshold);
  2930. $np->add_perfdata(label => "net_send", value => $value2, uom => 'KBps', threshold => $np->threshold);
  2931. $res = OK;
  2932. $output = "\"$vmname\" net receive=" . $value1 . " KBps, send=" . $value2 . " KBps";
  2933. }
  2934. }
  2935. return ($res, $output);
  2936. }
  2937. sub vm_disk_io_info
  2938. {
  2939. my ($vmname, $np, $subcommand) = @_;
  2940. my $res = CRITICAL;
  2941. my $output = 'HOST-VM IO Unknown error';
  2942. if (defined($subcommand))
  2943. {
  2944. if ($subcommand eq "USAGE")
  2945. {
  2946. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'disk', ('usage.average:*'));
  2947. if (defined($values))
  2948. {
  2949. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2950. $np->add_perfdata(label => "io_usage", value => $value, uom => 'MB', threshold => $np->threshold);
  2951. $output = "\"$vmname\" io usage=" . $value . " MB";
  2952. $res = $np->check_threshold(check => $value);
  2953. }
  2954. }
  2955. elsif ($subcommand eq "READ")
  2956. {
  2957. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'disk', ('read.average:*'));
  2958. if (defined($values))
  2959. {
  2960. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2961. $np->add_perfdata(label => "io_read", value => $value, uom => 'MB/s', threshold => $np->threshold);
  2962. $output = "\"$vmname\" io read=" . $value . " MB/s";
  2963. $res = $np->check_threshold(check => $value);
  2964. }
  2965. }
  2966. elsif ($subcommand eq "WRITE")
  2967. {
  2968. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'disk', ('write.average:*'));
  2969. if (defined($values))
  2970. {
  2971. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2972. $np->add_perfdata(label => "io_write", value => $value, uom => 'MB/s', threshold => $np->threshold);
  2973. $output = "\"$vmname\" io write=" . $value . " MB/s";
  2974. $res = $np->check_threshold(check => $value);
  2975. }
  2976. }
  2977. else
  2978. {
  2979. $res = CRITICAL;
  2980. $output = "HOST IO - unknown subcommand\n" . $np->opts->_help;
  2981. }
  2982. }
  2983. else
  2984. {
  2985. $values = return_host_vmware_performance_values($vmname, $defperfargs, 'disk', ('usage.average:*', 'read.average:*', 'write.average:*'));
  2986. if (defined($values))
  2987. {
  2988. my $value1 = simplify_number(convert_number($$values[0][0]->value) / 1024);
  2989. my $value2 = simplify_number(convert_number($$values[0][1]->value) / 1024);
  2990. my $value3 = simplify_number(convert_number($$values[0][2]->value) / 1024);
  2991. $np->add_perfdata(label => "io_usage", value => $value1, uom => 'MB', threshold => $np->threshold);
  2992. $np->add_perfdata(label => "io_read", value => $value2, uom => 'MB/s', threshold => $np->threshold);
  2993. $np->add_perfdata(label => "io_write", value => $value3, uom => 'MB/s', threshold => $np->threshold);
  2994. $res = OK;
  2995. $output = "\"$vmname\" io usage=" . $value1 . " MB, read=" . $value2 . " MB/s, write=" . $value3 . " MB/s";
  2996. }
  2997. }
  2998. return ($res, $output);
  2999. }
  3000. sub vm_runtime_info
  3001. {
  3002. my ($vmname, $np, $subcommand) = @_;
  3003. my $res = CRITICAL;
  3004. my $output = 'HOST-VM RUNTIME Unknown error';
  3005. my $runtime;
  3006. my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {name => $vmname}, properties => ['name', 'runtime', 'overallStatus', 'guest', 'configIssue']);
  3007. die "VMware machine \"" . $vmname . "\" does not exist\n" if (!defined($vm_view));
  3008. $runtime = $vm_view->runtime;
  3009. if (defined($subcommand))
  3010. {
  3011. if ($subcommand eq "CON")
  3012. {
  3013. $output = "\"$vmname\" connection state=" . $runtime->connectionState->val;
  3014. $res = OK if ($runtime->connectionState->val eq "connected");
  3015. }
  3016. elsif ($subcommand eq "CPU")
  3017. {
  3018. $output = "\"$vmname\" max cpu=" . $runtime->maxCpuUsage . " MHz";
  3019. $res = OK;
  3020. }
  3021. elsif ($subcommand eq "MEM")
  3022. {
  3023. $output = "\"$vmname\" max mem=" . $runtime->maxMemoryUsage . " MB";
  3024. $res = OK;
  3025. }
  3026. elsif ($subcommand eq "STATE")
  3027. {
  3028. my %vm_state_strings = ("poweredOn" => "UP", "poweredOff" => "DOWN", "suspended" => "SUSPENDED");
  3029. my $state = $vm_state_strings{$runtime->powerState->val};
  3030. $output = "\"$vmname\" run state=" . $state;
  3031. $res = OK if ($state eq "UP");
  3032. }
  3033. elsif ($subcommand eq "STATUS")
  3034. {
  3035. my $status = $vm_view->overallStatus->val;
  3036. $output = "\"$vmname\" overall status=" . $status;
  3037. $res = check_health_state($status);
  3038. }
  3039. elsif ($subcommand eq "CONSOLECONNECTIONS")
  3040. {
  3041. $output = "\"$vmname\" console connections=" . $runtime->numMksConnections;
  3042. $res = $np->check_threshold(check => $runtime->numMksConnections);
  3043. }
  3044. elsif ($subcommand eq "GUEST")
  3045. {
  3046. my %vm_guest_state = ("running" => "Running", "notRunning" => "Not running", "shuttingDown" => "Shutting down", "resetting" => "Resetting", "standby" => "Standby", "unknown" => "Unknown");
  3047. my $state = $vm_guest_state{$vm_view->guest->guestState};
  3048. $output = "\"$vmname\" guest state=" . $state;
  3049. $res = OK if ($state eq "Running");
  3050. }
  3051. elsif ($subcommand eq "TOOLS")
  3052. {
  3053. my $tools_status;
  3054. my $tools_runstate;
  3055. my $tools_version;
  3056. $tools_runstate = $vm_view->guest->toolsRunningStatus if (exists($vm_view->guest->{toolsRunningStatus}) && defined($vm_view->guest->toolsRunningStatus));
  3057. $tools_version = $vm_view->guest->toolsVersionStatus if (exists($vm_view->guest->{toolsVersionStatus}) && defined($vm_view->guest->toolsVersionStatus));
  3058. if (defined($tools_runstate) || defined($tools_version))
  3059. {
  3060. my %vm_tools_strings = ("guestToolsCurrent" => "Newest", "guestToolsNeedUpgrade" => "Old", "guestToolsNotInstalled" => "Not installed", "guestToolsUnmanaged" => "Unmanaged", "guestToolsExecutingScripts" => "Starting", "guestToolsNotRunning" => "Not running", "guestToolsRunning" => "Running");
  3061. $tools_status = $vm_tools_strings{$tools_runstate} . "-" if (defined($tools_runstate));
  3062. $tools_status .= $vm_tools_strings{$tools_version} . "-" if (defined($tools_version));
  3063. chop($tools_status);
  3064. $res = OK if (($tools_status eq "Running-Newest") || ($tools_status eq "Running-Unmanaged"));
  3065. }
  3066. else
  3067. {
  3068. my %vm_tools_strings = ("toolsNotInstalled" => "Not installed", "toolsNotRunning" => "Not running", "toolsOk" => "OK", "toolsOld" => "Old", "notDefined" => "Not defined");
  3069. $tools_status = $vm_view->guest->toolsStatus;
  3070. if (defined($tools_status))
  3071. {
  3072. $tools_status = $vm_tools_strings{$tools_status->val};
  3073. }
  3074. else
  3075. {
  3076. $tools_status = $vm_tools_strings{"notDefined"};
  3077. }
  3078. $res = OK if ($tools_status eq "OK");
  3079. }
  3080. $output = "\"$vmname\" tools status=" . $tools_status;
  3081. }
  3082. elsif ($subcommand eq "ISSUES")
  3083. {
  3084. my $issues = $vm_view->configIssue;
  3085. if (defined($issues))
  3086. {
  3087. $output = "\"$vmname\": ";
  3088. foreach (@$issues)
  3089. {
  3090. $output .= $_->fullFormattedMessage . "(caused by " . $_->userName . "); ";
  3091. }
  3092. }
  3093. else
  3094. {
  3095. $res = OK;
  3096. $output = "\"$vmname\" has no config issues";
  3097. }
  3098. }
  3099. else
  3100. {
  3101. $res = CRITICAL;
  3102. $output = "HOST-VM RUNTIME - unknown subcommand\n" . $np->opts->_help;
  3103. }
  3104. }
  3105. else
  3106. {
  3107. my %vm_state_strings = ("poweredOn" => "UP", "poweredOff" => "DOWN", "suspended" => "SUSPENDED");
  3108. my %vm_tools_status = ("toolsNotInstalled" => "Not installed", "toolsNotRunning" => "Not running", "toolsOk" => "OK", "toolsOld" => "Old");
  3109. my %vm_guest_state = ("running" => "Running", "notRunning" => "Not running", "shuttingDown" => "Shutting down", "resetting" => "Resetting", "standby" => "Standby", "unknown" => "Unknown");
  3110. $res = OK;
  3111. $output = "\"$vmname\" status=" . $vm_view->overallStatus->val . ", run state=" . $vm_state_strings{$runtime->powerState->val} . ", guest state=" . $vm_guest_state{$vm_view->guest->guestState} . ", max cpu=" . $runtime->maxCpuUsage . " MHz, max mem=" . $runtime->maxMemoryUsage . " MB, console connections=" . $runtime->numMksConnections . ", tools status=" . $vm_tools_status{$vm_view->guest->toolsStatus->val} . ", ";
  3112. my $issues = $vm_view->configIssue;
  3113. if (defined($issues))
  3114. {
  3115. $output .= @$issues . " config issue(s)";
  3116. }
  3117. else
  3118. {
  3119. $output .= "has no config issues";
  3120. }
  3121. }
  3122. return ($res, $output);
  3123. }
  3124. #==========================================================================| DC |============================================================================#
  3125. sub return_cluster_DRS_recommendations {
  3126. my ($np, $cluster_name) = @_;
  3127. my $res = OK;
  3128. my $output;
  3129. my @clusters;
  3130. if (defined($cluster_name))
  3131. {
  3132. my $cluster = Vim::find_entity_view(view_type => 'ClusterComputeResource', filter => $cluster_name, properties => ['name', 'recommendation']);
  3133. die "cluster \"" . $$cluster_name{"name"} . "\" does not exist\n" if (!defined($cluster));
  3134. push(@clusters, $cluster);
  3135. }
  3136. else
  3137. {
  3138. my $cluster = Vim::find_entity_views(view_type => 'ClusterComputeResource', properties => ['name', 'recommendation']);
  3139. die "Runtime error\n" if (!defined($cluster));
  3140. die "There are no clusters\n" if (!@$cluster);
  3141. @clusters = @$cluster;
  3142. }
  3143. my $rec_count = 0;
  3144. foreach my $cluster_view (@clusters)
  3145. {
  3146. my ($recommends) = $cluster_view->recommendation;
  3147. if (defined($recommends))
  3148. {
  3149. my $value = 0;
  3150. foreach my $recommend (@$recommends)
  3151. {
  3152. $value = $recommend->rating if ($recommend->rating > $value);
  3153. $output .= "(" . $recommend->rating . ") " . $recommend->reason . " : " . $recommend->reasonText . "; ";
  3154. }
  3155. $rec_count += @$recommends;
  3156. $res = $np->check_threshold(check => $value);
  3157. }
  3158. }
  3159. if (defined($output))
  3160. {
  3161. $output = "Recommendations:" . $output;
  3162. }
  3163. else
  3164. {
  3165. $output = "No recommendations";
  3166. }
  3167. $np->add_perfdata(label => "recommendations", value => $rec_count);
  3168. return ($res, $output);
  3169. }
  3170. sub dc_cpu_info
  3171. {
  3172. my ($np, $subcommand, $addopts) = @_;
  3173. my $res = CRITICAL;
  3174. my $output = 'DC CPU Unknown error';
  3175. my $quickStats;
  3176. $quickStats = $addopts =~ m/(^|\s|\t|,)\Qquickstats\E($|\s|\t|,)/ if (defined($addopts));
  3177. if (defined($subcommand))
  3178. {
  3179. if ($subcommand eq "USAGE")
  3180. {
  3181. my $value;
  3182. if (defined($quickStats))
  3183. {
  3184. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name', 'summary.hardware', 'summary.quickStats']);
  3185. die "Runtime error\n" if (!defined($host_views));
  3186. die "Datacenter does not contain any hosts\n" if (!@$host_views);
  3187. foreach my $host_view (@$host_views)
  3188. {
  3189. $values = $host_view->get_property('summary.quickStats');
  3190. my $hardinfo = $host_view->get_property('summary.hardware');
  3191. die "Can not retrieve required information from Host '" . $host_view->name . "'\n" if !(exists($values->{overallCpuUsage}) && defined($hardinfo));
  3192. $value += $values->overallCpuUsage / ($hardinfo->numCpuCores * $hardinfo->cpuMhz) * 100;
  3193. }
  3194. $value = simplify_number($value / @$host_views);
  3195. }
  3196. else
  3197. {
  3198. $values = return_dc_performance_values($defperfargs, 'cpu', ('usage.average'));
  3199. grep($value += convert_number($$_[0]->value) * 0.01, @$values) if (defined($values));
  3200. $value = simplify_number($value / @$values) if (defined($value));
  3201. }
  3202. if (defined($value))
  3203. {
  3204. $np->add_perfdata(label => "cpu_usage", value => $value, uom => '%', threshold => $np->threshold);
  3205. $output = "cpu usage=" . $value . " %";
  3206. $res = $np->check_threshold(check => $value);
  3207. }
  3208. }
  3209. elsif ($subcommand eq "USAGEMHZ")
  3210. {
  3211. my $value;
  3212. if (defined($quickStats))
  3213. {
  3214. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name', 'summary.quickStats']);
  3215. die "Runtime error\n" if (!defined($host_views));
  3216. die "Datacenter does not contain any hosts\n" if (!@$host_views);
  3217. foreach my $host_view (@$host_views)
  3218. {
  3219. $values = $host_view->get_property('summary.quickStats');
  3220. die "Can not retrieve required information from Host '" . $host_view->name . "'\n" if !(exists($values->{overallCpuUsage}));
  3221. $value += $values->overallCpuUsage;
  3222. }
  3223. $value = simplify_number($value);
  3224. }
  3225. else
  3226. {
  3227. $values = return_dc_performance_values($defperfargs, 'cpu', ('usagemhz.average'));
  3228. grep($value += convert_number($$_[0]->value), @$values);
  3229. $value = simplify_number($value) if (defined($value));
  3230. }
  3231. if (defined($value))
  3232. {
  3233. $np->add_perfdata(label => "cpu_usagemhz", value => $value, uom => 'MHz', threshold => $np->threshold);
  3234. $output = "cpu usagemhz=" . $value . " MHz";
  3235. $res = $np->check_threshold(check => $value);
  3236. }
  3237. }
  3238. else
  3239. {
  3240. $res = CRITICAL;
  3241. $output = "DC CPU - unknown subcommand\n" . $np->opts->_help;
  3242. }
  3243. }
  3244. else
  3245. {
  3246. my $value1 = 0;
  3247. my $value2 = 0;
  3248. if (defined($quickStats))
  3249. {
  3250. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name', 'summary.hardware', 'summary.quickStats']);
  3251. die "Runtime error\n" if (!defined($host_views));
  3252. die "Datacenter does not contain any hosts\n" if (!@$host_views);
  3253. foreach my $host_view (@$host_views)
  3254. {
  3255. $values = $host_view->get_property('summary.quickStats');
  3256. my $hardinfo = $host_view->get_property('summary.hardware');
  3257. die "Can not retrieve required information from Host '" . $host_view->name . "'\n" if !(exists($values->{overallCpuUsage}) && defined($hardinfo));
  3258. $value1 += $values->overallCpuUsage;
  3259. $value2 += $values->overallCpuUsage / ($hardinfo->numCpuCores * $hardinfo->cpuMhz) * 100;
  3260. }
  3261. $value1 = simplify_number($value1);
  3262. $value2 = simplify_number($value2 / @$host_views);
  3263. }
  3264. else
  3265. {
  3266. $values = return_dc_performance_values($defperfargs, 'cpu', ('usagemhz.average', 'usage.average'));
  3267. grep($value1 += convert_number($$_[0]->value), @$values);
  3268. grep($value2 += convert_number($$_[1]->value) * 0.01, @$values);
  3269. $value1 = simplify_number($value1);
  3270. $value2 = simplify_number($value2 / @$values);
  3271. }
  3272. if (defined($value1) && defined($value2))
  3273. {
  3274. $np->add_perfdata(label => "cpu_usagemhz", value => $value1, uom => 'MHz', threshold => $np->threshold);
  3275. $np->add_perfdata(label => "cpu_usage", value => $value2, uom => '%', threshold => $np->threshold);
  3276. $res = OK;
  3277. $output = "cpu usage=" . $value1 . " MHz (" . $value2 . "%)";
  3278. }
  3279. }
  3280. return ($res, $output);
  3281. }
  3282. sub dc_mem_info
  3283. {
  3284. my ($np, $subcommand, $addopts) = @_;
  3285. my $res = CRITICAL;
  3286. my $output = 'DC MEM Unknown error';
  3287. my $quickStats;
  3288. $quickStats = $addopts =~ m/(^|\s|\t|,)\Qquickstats\E($|\s|\t|,)/ if (defined($addopts));
  3289. if (defined($subcommand))
  3290. {
  3291. if ($subcommand eq "USAGE")
  3292. {
  3293. my $value;
  3294. if (defined($quickStats))
  3295. {
  3296. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name', 'summary.hardware', 'summary.quickStats']);
  3297. die "Runtime error\n" if (!defined($host_views));
  3298. die "Datacenter does not contain any hosts\n" if (!@$host_views);
  3299. foreach my $host_view (@$host_views)
  3300. {
  3301. $values = $host_view->get_property('summary.quickStats');
  3302. my $hardinfo = $host_view->get_property('summary.hardware');
  3303. die "Can not retrieve required information from Host '" . $host_view->name . "'\n" if !(exists($values->{overallMemoryUsage}) && defined($hardinfo));
  3304. $value += $values->overallMemoryUsage / ($hardinfo->memorySize / 1024 / 1024) * 100;
  3305. }
  3306. $value = simplify_number($value);
  3307. }
  3308. else
  3309. {
  3310. $values = return_dc_performance_values($defperfargs, 'mem', ('usage.average'));
  3311. grep($value += convert_number($$_[0]->value) * 0.01, @$values);
  3312. $value = simplify_number($value / @$values) if (defined($value));
  3313. }
  3314. if (defined($value))
  3315. {
  3316. $np->add_perfdata(label => "mem_usage", value => $value, uom => '%', threshold => $np->threshold);
  3317. $output = "mem usage=" . $value . " %";
  3318. $res = $np->check_threshold(check => $value);
  3319. }
  3320. }
  3321. elsif ($subcommand eq "USAGEMB")
  3322. {
  3323. my $value;
  3324. if (defined($quickStats))
  3325. {
  3326. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name', 'summary.quickStats']);
  3327. die "Runtime error\n" if (!defined($host_views));
  3328. die "Datacenter does not contain any hosts\n" if (!@$host_views);
  3329. foreach my $host_view (@$host_views)
  3330. {
  3331. $values = $host_view->get_property('summary.quickStats');
  3332. die "Can not retrieve required information from Host '" . $host_view->name . "'\n" if !(exists($values->{overallMemoryUsage}));
  3333. $value += $values->overallMemoryUsage;
  3334. }
  3335. $value = simplify_number($value);
  3336. }
  3337. else
  3338. {
  3339. $values = return_dc_performance_values($defperfargs, 'mem', ('consumed.average'));
  3340. grep($value += convert_number($$_[0]->value) / 1024, @$values);
  3341. $value = simplify_number($value) if (defined($value));
  3342. }
  3343. if (defined($value))
  3344. {
  3345. $np->add_perfdata(label => "mem_usagemb", value => $value, uom => 'MB', threshold => $np->threshold);
  3346. $output = "mem usage=" . $value . " MB";
  3347. $res = $np->check_threshold(check => $value);
  3348. }
  3349. }
  3350. elsif ($subcommand eq "SWAP")
  3351. {
  3352. $values = return_dc_performance_values($defperfargs, 'mem', ('swapused.average'));
  3353. if (defined($values))
  3354. {
  3355. my $value = 0;
  3356. grep($value += convert_number($$_[0]->value) / 1024, @$values);
  3357. $value = simplify_number($value);
  3358. $np->add_perfdata(label => "mem_swap", value => $value, uom => 'MB', threshold => $np->threshold);
  3359. $output = "swap usage=" . $value . " MB";
  3360. $res = $np->check_threshold(check => $value);
  3361. }
  3362. }
  3363. elsif ($subcommand eq "OVERHEAD")
  3364. {
  3365. $values = return_dc_performance_values($defperfargs, 'mem', ('overhead.average'));
  3366. if (defined($values))
  3367. {
  3368. my $value = 0;
  3369. grep($value += convert_number($$_[0]->value) / 1024, @$values);
  3370. $value = simplify_number($value);
  3371. $np->add_perfdata(label => "mem_overhead", value => $value, uom => 'MB', threshold => $np->threshold);
  3372. $output = "overhead=" . $value . " MB";
  3373. $res = $np->check_threshold(check => $value);
  3374. }
  3375. }
  3376. elsif ($subcommand eq "OVERALL")
  3377. {
  3378. $values = return_dc_performance_values($defperfargs, 'mem', ('consumed.average', 'overhead.average'));
  3379. if (defined($values))
  3380. {
  3381. my $value = 0;
  3382. grep($value += (convert_number($$_[0]->value) + convert_number($$_[1]->value)) / 1024, @$values);
  3383. $value = simplify_number($value);
  3384. $np->add_perfdata(label => "mem_overall", value => $value, uom => 'MB', threshold => $np->threshold);
  3385. $output = "overall=" . $value . " MB";
  3386. $res = $np->check_threshold(check => $value);
  3387. }
  3388. }
  3389. elsif ($subcommand eq "MEMCTL")
  3390. {
  3391. $values = return_dc_performance_values($defperfargs, 'mem', ('vmmemctl.average'));
  3392. if (defined($values))
  3393. {
  3394. my $value = 0;
  3395. grep($value += convert_number($$_[0]->value) / 1024, @$values);
  3396. $value = simplify_number($value);
  3397. $np->add_perfdata(label => "mem_memctl", value => $value, uom => 'MB', threshold => $np->threshold);
  3398. $output = "memctl=" . $value . " MB";
  3399. $res = $np->check_threshold(check => $value);
  3400. }
  3401. }
  3402. else
  3403. {
  3404. $res = CRITICAL;
  3405. $output = "DC MEM - unknown subcommand\n" . $np->opts->_help;
  3406. }
  3407. }
  3408. else
  3409. {
  3410. $values = return_dc_performance_values($defperfargs, 'mem', ('consumed.average', 'usage.average', 'overhead.average', 'swapused.average', 'vmmemctl.average'));
  3411. if (defined($values))
  3412. {
  3413. my $value1 = 0;
  3414. my $value2 = 0;
  3415. my $value3 = 0;
  3416. my $value4 = 0;
  3417. my $value5 = 0;
  3418. grep($value1 += convert_number($$_[0]->value) / 1024, @$values);
  3419. grep($value2 += convert_number($$_[1]->value) * 0.01, @$values);
  3420. grep($value3 += convert_number($$_[2]->value) / 1024, @$values);
  3421. grep($value4 += convert_number($$_[3]->value) / 1024, @$values);
  3422. grep($value5 += convert_number($$_[4]->value) / 1024, @$values);
  3423. $value1 = simplify_number($value1);
  3424. $value2 = simplify_number($value2 / @$values);
  3425. $value3 = simplify_number($value3);
  3426. $value4 = simplify_number($value4);
  3427. $value5 = simplify_number($value5);
  3428. $np->add_perfdata(label => "mem_usagemb", value => $value1, uom => 'MB', threshold => $np->threshold);
  3429. $np->add_perfdata(label => "mem_usage", value => $value2, uom => '%', threshold => $np->threshold);
  3430. $np->add_perfdata(label => "mem_overhead", value => $value3, uom => 'MB', threshold => $np->threshold);
  3431. $np->add_perfdata(label => "mem_swap", value => $value4, uom => 'MB', threshold => $np->threshold);
  3432. $np->add_perfdata(label => "mem_memctl", value => $value5, uom => 'MB', threshold => $np->threshold);
  3433. $res = OK;
  3434. $output = "mem usage=" . $value1 . " MB (" . $value2 . "%), overhead=" . $value3 . " MB, swapped=" . $value4 . " MB, memctl=" . $value5 . " MB";
  3435. }
  3436. }
  3437. return ($res, $output);
  3438. }
  3439. sub dc_net_info
  3440. {
  3441. my ($np, $subcommand) = @_;
  3442. my $res = CRITICAL;
  3443. my $output = 'DC NET Unknown error';
  3444. if (defined($subcommand))
  3445. {
  3446. if ($subcommand eq "USAGE")
  3447. {
  3448. $values = return_dc_performance_values($defperfargs, 'net', ('usage.average:*'));
  3449. if (defined($values))
  3450. {
  3451. my $value = 0;
  3452. grep($value += convert_number($$_[0]->value), @$values);
  3453. $value = simplify_number($value);
  3454. $np->add_perfdata(label => "net_usage", value => $value, uom => 'KBps', threshold => $np->threshold);
  3455. $output = "net usage=" . $value . " KBps";
  3456. $res = $np->check_threshold(check => $value);
  3457. }
  3458. }
  3459. elsif ($subcommand eq "RECEIVE")
  3460. {
  3461. $values = return_dc_performance_values($defperfargs, 'net', ('received.average:*'));
  3462. if (defined($values))
  3463. {
  3464. my $value = 0;
  3465. grep($value += convert_number($$_[0]->value), @$values);
  3466. $value = simplify_number($value);
  3467. $np->add_perfdata(label => "net_receive", value => $value, uom => 'KBps', threshold => $np->threshold);
  3468. $output = "net receive=" . $value . " KBps";
  3469. $res = $np->check_threshold(check => $value);
  3470. }
  3471. }
  3472. elsif ($subcommand eq "SEND")
  3473. {
  3474. $values = return_dc_performance_values($defperfargs, 'net', ('transmitted.average:*'));
  3475. if (defined($values))
  3476. {
  3477. my $value = 0;
  3478. grep($value += convert_number($$_[0]->value), @$values);
  3479. $value = simplify_number($value);
  3480. $np->add_perfdata(label => "net_send", value => $value, uom => 'KBps', threshold => $np->threshold);
  3481. $output = "net send=" . $value . " KBps";
  3482. $res = $np->check_threshold(check => $value);
  3483. }
  3484. }
  3485. else
  3486. {
  3487. $res = CRITICAL;
  3488. $output = "DC NET - unknown subcommand\n" . $np->opts->_help;
  3489. }
  3490. }
  3491. else
  3492. {
  3493. $values = return_dc_performance_values($defperfargs, 'net', ('received.average:*', 'transmitted.average:*'));
  3494. if (defined($values))
  3495. {
  3496. my $value1 = 0;
  3497. my $value2 = 0;
  3498. grep($value1 += convert_number($$_[0]->value), @$values);
  3499. grep($value2 += convert_number($$_[1]->value), @$values);
  3500. $value1 = simplify_number($value1);
  3501. $value2 = simplify_number($value2);
  3502. $np->add_perfdata(label => "net_receive", value => $value1, uom => 'KBps', threshold => $np->threshold);
  3503. $np->add_perfdata(label => "net_send", value => $value2, uom => 'KBps', threshold => $np->threshold);
  3504. $res = OK;
  3505. $output = "net receive=" . $value1 . " KBps, send=" . $value2 . " KBps";
  3506. }
  3507. }
  3508. return ($res, $output);
  3509. }
  3510. sub dc_list_vm_volumes_info
  3511. {
  3512. my ($np, $subcommand, $blacklist, $perc, $addopts) = @_;
  3513. my $dc_views = Vim::find_entity_views(view_type => 'Datacenter', properties => ['datastore']);
  3514. die "There are no Datacenter\n" if (!defined($dc_views));
  3515. my @datastores;
  3516. foreach my $dc (@$dc_views)
  3517. {
  3518. push(@datastores, @{$dc->datastore}) if (defined($dc->datastore));
  3519. }
  3520. return datastore_volumes_info(\@datastores, $np, $subcommand, $blacklist, $perc, $addopts);
  3521. }
  3522. sub dc_disk_io_info
  3523. {
  3524. my ($np, $subcommand) = @_;
  3525. my $res = CRITICAL;
  3526. my $output = 'DC IO Unknown error';
  3527. if (defined($subcommand))
  3528. {
  3529. if ($subcommand eq "ABORTED")
  3530. {
  3531. $values = return_dc_performance_values($defperfargs, 'disk', ('commandsAborted.summation:*'));
  3532. if (defined($values))
  3533. {
  3534. my $value = 0;
  3535. grep($value += convert_number($$_[0]->value), @$values);
  3536. $value = simplify_number($value, 0);
  3537. $np->add_perfdata(label => "io_aborted", value => $value, threshold => $np->threshold);
  3538. $output = "io commands aborted=" . $value;
  3539. $res = $np->check_threshold(check => $value);
  3540. }
  3541. }
  3542. elsif ($subcommand eq "RESETS")
  3543. {
  3544. $values = return_dc_performance_values($defperfargs, 'disk', ('busResets.summation:*'));
  3545. if (defined($values))
  3546. {
  3547. my $value = 0;
  3548. grep($value += convert_number($$_[0]->value), @$values);
  3549. $value = simplify_number($value, 0);
  3550. $np->add_perfdata(label => "io_busresets", value => $value, threshold => $np->threshold);
  3551. $output = "io bus resets=" . $value;
  3552. $res = $np->check_threshold(check => $value);
  3553. }
  3554. }
  3555. elsif ($subcommand eq "READ")
  3556. {
  3557. $values = return_dc_performance_values($defperfargs, 'disk', ('totalReadLatency.average:*'));
  3558. if (defined($values))
  3559. {
  3560. my $value = 0;
  3561. grep($value += convert_number($$_[0]->value), @$values);
  3562. $value = simplify_number($value, 0);
  3563. $np->add_perfdata(label => "io_read", value => $value, uom => 'ms', threshold => $np->threshold);
  3564. $output = "io read latency=" . $value . " ms";
  3565. $res = $np->check_threshold(check => $value);
  3566. }
  3567. }
  3568. elsif ($subcommand eq "WRITE")
  3569. {
  3570. $values = return_dc_performance_values($defperfargs, 'disk', ('totalWriteLatency.average:*'));
  3571. if (defined($values))
  3572. {
  3573. my $value = 0;
  3574. grep($value += convert_number($$_[0]->value), @$values);
  3575. $value = simplify_number($value, 0);
  3576. $np->add_perfdata(label => "io_write", value => $value, uom => 'ms', threshold => $np->threshold);
  3577. $output = "io write latency=" . $value . " ms";
  3578. $res = $np->check_threshold(check => $value);
  3579. }
  3580. }
  3581. elsif ($subcommand eq "KERNEL")
  3582. {
  3583. $values = return_dc_performance_values($defperfargs, 'disk', ('kernelLatency.average:*'));
  3584. if (defined($values))
  3585. {
  3586. my $value = 0;
  3587. grep($value += convert_number($$_[0]->value), @$values);
  3588. $value = simplify_number($value, 0);
  3589. $np->add_perfdata(label => "io_kernel", value => $value, uom => 'ms', threshold => $np->threshold);
  3590. $output = "io kernel latency=" . $value . " ms";
  3591. $res = $np->check_threshold(check => $value);
  3592. }
  3593. }
  3594. elsif ($subcommand eq "DEVICE")
  3595. {
  3596. $values = return_dc_performance_values($defperfargs, 'disk', ('deviceLatency.average:*'));
  3597. if (defined($values))
  3598. {
  3599. my $value = 0;
  3600. grep($value += convert_number($$_[0]->value), @$values);
  3601. $value = simplify_number($value, 0);
  3602. $np->add_perfdata(label => "io_device", value => $value, uom => 'ms', threshold => $np->threshold);
  3603. $output = "io device latency=" . $value . " ms";
  3604. $res = $np->check_threshold(check => $value);
  3605. }
  3606. }
  3607. elsif ($subcommand eq "QUEUE")
  3608. {
  3609. $values = return_dc_performance_values($defperfargs, 'disk', ('queueLatency.average:*'));
  3610. if (defined($values))
  3611. {
  3612. my $value = 0;
  3613. grep($value += convert_number($$_[0]->value), @$values);
  3614. $value = simplify_number($value, 0);
  3615. $np->add_perfdata(label => "io_queue", value => $value, uom => 'ms', threshold => $np->threshold);
  3616. $output = "io queue latency=" . $value . " ms";
  3617. $res = $np->check_threshold(check => $value);
  3618. }
  3619. }
  3620. else
  3621. {
  3622. $res = CRITICAL;
  3623. $output = "DC IO - unknown subcommand\n" . $np->opts->_help;
  3624. }
  3625. }
  3626. else
  3627. {
  3628. $values = return_dc_performance_values($defperfargs, 'disk', ('commandsAborted.summation:*', 'busResets.summation:*', 'totalReadLatency.average:*', 'totalWriteLatency.average:*', 'kernelLatency.average:*', 'deviceLatency.average:*', 'queueLatency.average:*'));
  3629. if (defined($values))
  3630. {
  3631. my $value1 = 0;
  3632. my $value2 = 0;
  3633. my $value3 = 0;
  3634. my $value4 = 0;
  3635. my $value5 = 0;
  3636. my $value6 = 0;
  3637. my $value7 = 0;
  3638. grep($value1 += convert_number($$_[0]->value), @$values);
  3639. grep($value2 += convert_number($$_[1]->value), @$values);
  3640. grep($value3 += convert_number($$_[2]->value), @$values);
  3641. grep($value4 += convert_number($$_[3]->value), @$values);
  3642. grep($value5 += convert_number($$_[4]->value), @$values);
  3643. grep($value6 += convert_number($$_[5]->value), @$values);
  3644. grep($value7 += convert_number($$_[6]->value), @$values);
  3645. $value1 = simplify_number($value1, 0);
  3646. $value2 = simplify_number($value2, 0);
  3647. $value3 = simplify_number($value3, 0);
  3648. $value4 = simplify_number($value4, 0);
  3649. $value5 = simplify_number($value5, 0);
  3650. $value6 = simplify_number($value6, 0);
  3651. $value7 = simplify_number($value7, 0);
  3652. $np->add_perfdata(label => "io_aborted", value => $value1, threshold => $np->threshold);
  3653. $np->add_perfdata(label => "io_busresets", value => $value2, threshold => $np->threshold);
  3654. $np->add_perfdata(label => "io_read", value => $value3, uom => 'ms', threshold => $np->threshold);
  3655. $np->add_perfdata(label => "io_write", value => $value4, uom => 'ms', threshold => $np->threshold);
  3656. $np->add_perfdata(label => "io_kernel", value => $value5, uom => 'ms', threshold => $np->threshold);
  3657. $np->add_perfdata(label => "io_device", value => $value6, uom => 'ms', threshold => $np->threshold);
  3658. $np->add_perfdata(label => "io_queue", value => $value7, uom => 'ms', threshold => $np->threshold);
  3659. $res = OK;
  3660. $output = "io commands aborted=" . $value1 . ", io bus resets=" . $value2 . ", io read latency=" . $value3 . " ms, write latency=" . $value4 . " ms, kernel latency=" . $value5 . " ms, device latency=" . $value6 . " ms, queue latency=" . $value7 ." ms";
  3661. }
  3662. }
  3663. return ($res, $output);
  3664. }
  3665. sub dc_runtime_info
  3666. {
  3667. my ($np, $subcommand, $blacklist) = @_;
  3668. my $res = CRITICAL;
  3669. my $output = 'DC RUNTIME Unknown error';
  3670. my $runtime;
  3671. if (defined($subcommand))
  3672. {
  3673. if (($subcommand eq "LIST") || ($subcommand eq "LISTVM"))
  3674. {
  3675. my %vm_state_strings = ("poweredOn" => "UP", "poweredOff" => "DOWN", "suspended" => "SUSPENDED");
  3676. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', properties => ['name', 'runtime']);
  3677. die "Runtime error\n" if (!defined($vm_views));
  3678. die "There are no VMs.\n" if (!@$vm_views);
  3679. my $up = 0;
  3680. $output = '';
  3681. foreach my $vm (@$vm_views) {
  3682. my $vm_state = $vm_state_strings{$vm->runtime->powerState->val};
  3683. if ($vm_state eq "UP")
  3684. {
  3685. $up++;
  3686. $output .= $vm->name . "(UP), ";
  3687. }
  3688. else
  3689. {
  3690. $output = $vm->name . "(" . $vm_state . "), " . $output;
  3691. }
  3692. }
  3693. chop($output);
  3694. chop($output);
  3695. $res = OK;
  3696. $output = $up . "/" . @$vm_views . " VMs up: " . $output;
  3697. $np->add_perfdata(label => "vmcount", value => $up, uom => 'units', threshold => $np->threshold);
  3698. $res = $np->check_threshold(check => $up) if (defined($np->threshold));
  3699. }
  3700. elsif ($subcommand eq "LISTHOST")
  3701. {
  3702. my %host_state_strings = ("unknown" => "UNKNOWN", "poweredOn" => "UP", "poweredOff" => "DOWN", "suspended" => "SUSPENDED", "standBy" => "STANDBY", "MaintenanceMode" => "Maintenance Mode");
  3703. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name', 'runtime.powerState']);
  3704. die "Runtime error\n" if (!defined($host_views));
  3705. die "There are no VMs.\n" if (!@$host_views);
  3706. my $up = 0;
  3707. my $unknown = 0;
  3708. $output = '';
  3709. foreach my $host (@$host_views) {
  3710. $host->update_view_data(['name', 'runtime.powerState']);
  3711. my $host_state = $host_state_strings{$host->get_property('runtime.powerState')->val};
  3712. $unknown += $host_state eq "UNKNOWN";
  3713. if ($host_state eq "UP") {
  3714. $up++;
  3715. $output .= $host->name . "(UP), ";
  3716. }
  3717. else
  3718. {
  3719. $output = $host->name . "(" . $host_state . "), " . $output;
  3720. }
  3721. }
  3722. chop($output);
  3723. chop($output);
  3724. $res = OK;
  3725. $output = $up . "/" . @$host_views . " Hosts up: " . $output;
  3726. $np->add_perfdata(label => "hostcount", value => $up, uom => 'units', threshold => $np->threshold);
  3727. $res = $np->check_threshold(check => $up) if (defined($np->threshold));
  3728. $res = UNKNOWN if ($res == OK && $unknown);
  3729. }
  3730. elsif ($subcommand eq "LISTCLUSTER")
  3731. {
  3732. my %cluster_state_strings = ("gray" => "UNKNOWN", "green" => "GREEN", "red" => "RED", "yellow" => "YELLOW");
  3733. my $cluster_views = Vim::find_entity_views(view_type => 'ClusterComputeResource', properties => ['name', 'overallStatus']);
  3734. die "Runtime error\n" if (!defined($cluster_views));
  3735. die "There are no Clusters.\n" if (!@$cluster_views);
  3736. my $green = 0;
  3737. my $unknown = 0;
  3738. $output = '';
  3739. foreach my $cluster (@$cluster_views) {
  3740. $cluster->update_view_data(['name', 'overallStatus']);
  3741. my $cluster_state = $cluster_state_strings{$cluster->get_property('overallStatus')->val};
  3742. $unknown += $cluster_state eq "UNKNOWN";
  3743. if ($cluster_state eq "GREEN") {
  3744. $green++;
  3745. $output .= $cluster->name . "(GREEN), ";
  3746. }
  3747. else
  3748. {
  3749. $output = $cluster->name . "(" . $cluster_state . "), " . $output;
  3750. }
  3751. }
  3752. chop($output);
  3753. chop($output);
  3754. $res = OK;
  3755. $output = $green . "/" . @$cluster_views . " Cluster green: " . $output;
  3756. $np->add_perfdata(label => "clustercount", value => $green, uom => 'units', threshold => $np->threshold);
  3757. $res = $np->check_threshold(check => $green) if (defined($np->threshold));
  3758. $res = UNKNOWN if ($res == OK && $unknown);
  3759. }
  3760. elsif ($subcommand eq "TOOLS")
  3761. {
  3762. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', properties => ['name', 'runtime.powerState', 'summary.guest']);
  3763. die "Runtime error\n" if (!defined($vm_views));
  3764. die "There are no VMs.\n" if (!@$vm_views);
  3765. $output = '';
  3766. my $tools_ok = 0;
  3767. my $vms_up = 0;
  3768. foreach my $vm (@$vm_views) {
  3769. my $name = $vm->name;
  3770. if (defined($blacklist))
  3771. {
  3772. next if ($blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  3773. }
  3774. if ($vm->get_property('runtime.powerState')->val eq "poweredOn")
  3775. {
  3776. my $vm_guest = $vm->get_property('summary.guest');
  3777. my $tools_status;
  3778. my $tools_runstate;
  3779. my $tools_version;
  3780. $tools_runstate = $vm_guest->toolsRunningStatus if (exists($vm_guest->{toolsRunningStatus}) && defined($vm_guest->toolsRunningStatus));
  3781. $tools_version = $vm_guest->toolsVersionStatus if (exists($vm_guest->{toolsVersionStatus}) && defined($vm_guest->toolsVersionStatus));
  3782. $vms_up++;
  3783. if (defined($tools_runstate) || defined($tools_version))
  3784. {
  3785. my %vm_tools_strings = ("guestToolsCurrent" => "Newest", "guestToolsNeedUpgrade" => "Old", "guestToolsNotInstalled" => "Not installed", "guestToolsUnmanaged" => "Unmanaged", "guestToolsExecutingScripts" => "Starting", "guestToolsNotRunning" => "Not running", "guestToolsRunning" => "Running");
  3786. $tools_status = $vm_tools_strings{$tools_runstate} . "-" if (defined($tools_runstate));
  3787. $tools_status .= $vm_tools_strings{$tools_version} . "-" if (defined($tools_version));
  3788. chop($tools_status);
  3789. if ($tools_status eq "Running-Newest")
  3790. {
  3791. $output .= $name . "(Running-Newest), ";
  3792. $tools_ok++;
  3793. }
  3794. else
  3795. {
  3796. $output = $name . "(" . $tools_status . "), " . $output;
  3797. }
  3798. }
  3799. else
  3800. {
  3801. my %vm_tools_strings = ("toolsNotInstalled" => "Not installed", "toolsNotRunning" => "Not running", "toolsOk" => "OK", "toolsOld" => "Old", "notDefined" => "Not defined");
  3802. $tools_status = $vm_guest->toolsStatus;
  3803. if (defined($tools_status))
  3804. {
  3805. $tools_status = $vm_tools_strings{$tools_status->val};
  3806. if ($tools_status eq "OK")
  3807. {
  3808. $output .= $name . "(OK), ";
  3809. $tools_ok++;
  3810. }
  3811. else
  3812. {
  3813. $output = $name . "(" . $tools_status . "), " . $output;
  3814. }
  3815. }
  3816. else
  3817. {
  3818. $output = $name . "(" . $vm_tools_strings{"notDefined"} . "), " . $output;
  3819. }
  3820. }
  3821. }
  3822. }
  3823. chop($output);
  3824. chop($output);
  3825. if ($vms_up)
  3826. {
  3827. $tools_ok /= $vms_up / 100;
  3828. }
  3829. else
  3830. {
  3831. $tools_ok = 100;
  3832. }
  3833. $res = $np->check_threshold(check => $tools_ok) if (defined($np->threshold));
  3834. $np->add_perfdata(label => "toolsok", value => $tools_ok, uom => '%', threshold => $np->threshold);
  3835. }
  3836. elsif ($subcommand eq "STATUS")
  3837. {
  3838. my $dc_views = Vim::find_entity_views(view_type => 'Datacenter', properties => ['name', 'overallStatus']);
  3839. die "There are no Datacenter\n" if (!defined($dc_views));
  3840. $res = OK;
  3841. $output = '';
  3842. foreach my $dc (@$dc_views) {
  3843. if (defined($dc->overallStatus))
  3844. {
  3845. my $status = $dc->overallStatus->val;
  3846. $output .= $dc->name . " overall status=" . $status . ", ";
  3847. $status = check_health_state($status);
  3848. $res = UNKNOWN if ($status == UNKNOWN);
  3849. $res = Nagios::Plugin::Functions::max_state($res, $status) if (($res != UNKNOWN) || ($status != OK));
  3850. }
  3851. else
  3852. {
  3853. $output .= "Insufficient rights to access " . $dc->name . " status info on the DC, ";
  3854. $res = Nagios::Plugin::Functions::max_state($res, WARNING);
  3855. }
  3856. }
  3857. if ($output) {
  3858. chop($output);
  3859. chop($output);
  3860. }
  3861. }
  3862. elsif ($subcommand eq "ISSUES")
  3863. {
  3864. my $dc_views = Vim::find_entity_views(view_type => 'Datacenter', properties => ['name', 'configIssue']);
  3865. die "There are no Datacenter\n" if (!defined($dc_views));
  3866. my $issues_count = 0;
  3867. $output = '';
  3868. foreach my $dc (@$dc_views) {
  3869. my $issues = $dc->configIssue;
  3870. if (defined($issues))
  3871. {
  3872. foreach (@$issues)
  3873. {
  3874. if (defined($blacklist))
  3875. {
  3876. my $name = ref($_);
  3877. next if ($blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  3878. }
  3879. $output .= format_issue($_) . "(" . $dc->name . "); ";
  3880. $issues_count++;
  3881. }
  3882. }
  3883. }
  3884. if ($output eq '')
  3885. {
  3886. $res = OK;
  3887. $output = 'No config issues';
  3888. }
  3889. $np->add_perfdata(label => "issues", value => $issues_count);
  3890. }
  3891. else
  3892. {
  3893. $res = CRITICAL;
  3894. $output = "HOST RUNTIME - unknown subcommand\n" . $np->opts->_help;
  3895. }
  3896. }
  3897. else
  3898. {
  3899. my $dc_views = Vim::find_entity_views(view_type => 'Datacenter', properties => ['name', 'overallStatus', 'configIssue']);
  3900. die "There are no Datacenter\n" if (!defined($dc_views));
  3901. my %host_maintenance_state = (0 => "no", 1 => "yes");
  3902. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', properties => ['name', 'runtime.powerState', 'config.template']);
  3903. die "Runtime error\n" if (!defined($vm_views));
  3904. my $up = 0;
  3905. my $templ = 0;
  3906. if (@$vm_views)
  3907. {
  3908. my $totalvms = @$vm_views;
  3909. foreach my $vm (@$vm_views) {
  3910. $up += $vm->get_property('runtime.powerState')->val eq "poweredOn";
  3911. $templ += $vm->{'config.template'} eq "true";
  3912. }
  3913. $totalvms -= $templ;
  3914. $output = $up . "/" . $totalvms . " VMs up (" . $templ . " templates), ";
  3915. }
  3916. else
  3917. {
  3918. $output = "No VMs installed, ";
  3919. }
  3920. $np->add_perfdata(label => "vmcount", value => $up, uom => 'units', threshold => $np->threshold);
  3921. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name', 'runtime.powerState']);
  3922. die "Runtime error\n" if (!defined($host_views));
  3923. $up = 0;
  3924. if (@$host_views)
  3925. {
  3926. foreach my $host (@$host_views) {
  3927. $up += $host->get_property('runtime.powerState')->val eq "poweredOn"
  3928. }
  3929. $output .= $up . "/" . @$host_views . " Hosts up, ";
  3930. }
  3931. else
  3932. {
  3933. $output .= "there are no hosts, ";
  3934. }
  3935. $np->add_perfdata(label => "hostcount", value => $up, uom => 'units');
  3936. $res = OK;
  3937. foreach my $dc (@$dc_views) {
  3938. $output .= $dc->name . " overall status=" . $dc->overallStatus->val . ", " if (defined($dc->overallStatus));
  3939. }
  3940. my $issue_count = 0;
  3941. foreach my $dc (@$dc_views) {
  3942. my $issues = $dc->configIssue;
  3943. $issue_count += @$issues if (defined($issues));
  3944. }
  3945. if ($issue_count)
  3946. {
  3947. $output .= $issue_count . " config issue(s)";
  3948. $np->add_perfdata(label => "config_issues", value => $issue_count);
  3949. }
  3950. else
  3951. {
  3952. $output .= "no config issues";
  3953. $np->add_perfdata(label => "config_issues", value => 0);
  3954. }
  3955. }
  3956. return ($res, $output);
  3957. }
  3958. #=====================================================================| Cluster |============================================================================#
  3959. sub cluster_cpu_info
  3960. {
  3961. my ($cluster, $np, $subcommand) = @_;
  3962. my $res = CRITICAL;
  3963. my $output = 'CLUSTER CPU Unknown error';
  3964. if (defined($subcommand))
  3965. {
  3966. if ($subcommand eq "USAGE")
  3967. {
  3968. $values = return_cluster_performance_values($cluster, $defperfargs, 'cpu', ('usage.average'));
  3969. if (defined($values))
  3970. {
  3971. my $value = simplify_number(convert_number($$values[0][0]->value) * 0.01);
  3972. $np->add_perfdata(label => "cpu_usage", value => $value, uom => '%', threshold => $np->threshold);
  3973. $output = "cpu usage=" . $value . " %";
  3974. $res = $np->check_threshold(check => $value);
  3975. }
  3976. }
  3977. elsif ($subcommand eq "USAGEMHZ")
  3978. {
  3979. $values = return_cluster_performance_values($cluster, $defperfargs, 'cpu', ('usagemhz.average'));
  3980. if (defined($values))
  3981. {
  3982. my $value = simplify_number(convert_number($$values[0][0]->value));
  3983. $np->add_perfdata(label => "cpu_usagemhz", value => $value, uom => 'MHz', threshold => $np->threshold);
  3984. $output = "cpu usagemhz=" . $value . " MHz";
  3985. $res = $np->check_threshold(check => $value);
  3986. }
  3987. }
  3988. else
  3989. {
  3990. $res = CRITICAL;
  3991. $output = "CLUSTER CPU - unknown subcommand\n" . $np->opts->_help;
  3992. }
  3993. }
  3994. else
  3995. {
  3996. $values = return_cluster_performance_values($cluster, $defperfargs, 'cpu', ('usagemhz.average', 'usage.average'));
  3997. if (defined($values))
  3998. {
  3999. my $value1 = simplify_number(convert_number($$values[0][0]->value));
  4000. my $value2 = simplify_number(convert_number($$values[0][1]->value) * 0.01);
  4001. $np->add_perfdata(label => "cpu_usagemhz", value => $value1, uom => 'MHz', threshold => $np->threshold);
  4002. $np->add_perfdata(label => "cpu_usage", value => $value2, uom => '%', threshold => $np->threshold);
  4003. $res = OK;
  4004. $output = "cpu usage=" . $value1 . " MHz (" . $value2 . "%)";
  4005. }
  4006. }
  4007. return ($res, $output);
  4008. }
  4009. sub cluster_mem_info
  4010. {
  4011. my ($cluster, $np, $subcommand, $addopts) = @_;
  4012. my $res = CRITICAL;
  4013. my $output = 'CLUSTER MEM Unknown error';
  4014. my $outputlist;
  4015. $outputlist = $addopts =~ m/(^|\s|\t|,)\Qlistvm\E($|\s|\t|,)/ if (defined($addopts));
  4016. if (defined($subcommand))
  4017. {
  4018. if ($subcommand eq "USAGE")
  4019. {
  4020. $values = return_cluster_performance_values($cluster, $defperfargs, 'mem', ('usage.average'));
  4021. if (defined($values))
  4022. {
  4023. my $value = simplify_number(convert_number($$values[0][0]->value) * 0.01);
  4024. $np->add_perfdata(label => "mem_usage", value => $value, uom => '%', threshold => $np->threshold);
  4025. $output = "mem usage=" . $value . " %";
  4026. $res = $np->check_threshold(check => $value);
  4027. }
  4028. }
  4029. elsif ($subcommand eq "USAGEMB")
  4030. {
  4031. $values = return_cluster_performance_values($cluster, $defperfargs, 'mem', ('consumed.average'));
  4032. if (defined($values))
  4033. {
  4034. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  4035. $np->add_perfdata(label => "mem_usagemb", value => $value, uom => 'MB', threshold => $np->threshold);
  4036. $output = "mem usage=" . $value . " MB";
  4037. $res = $np->check_threshold(check => $value);
  4038. }
  4039. }
  4040. elsif ($subcommand eq "SWAP")
  4041. {
  4042. my $cluster_view;
  4043. ($cluster_view, $values) = return_cluster_performance_values($cluster, $defperfargs, 'mem', ('swapused.average'));
  4044. if (defined($values))
  4045. {
  4046. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  4047. $np->add_perfdata(label => "mem_swap", value => $value, uom => 'MB', threshold => $np->threshold);
  4048. $output = "swap usage=" . $value . " MB: ";
  4049. $res = $np->check_threshold(check => $value);
  4050. if ($res != OK && $outputlist)
  4051. {
  4052. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $$cluster_view[0], properties => ['name', 'runtime.powerState']);
  4053. die "Runtime error\n" if (!defined($vm_views));
  4054. die "There are no VMs.\n" if (!@$vm_views);
  4055. my @vms = ();
  4056. foreach my $vm (@$vm_views)
  4057. {
  4058. push(@vms, $vm) if ($vm->get_property('runtime.powerState')->val eq "poweredOn");
  4059. }
  4060. $values = generic_performance_values(\@vms, $defperfargs, 'mem', ('swapped.average'));
  4061. if (defined($values))
  4062. {
  4063. foreach my $index (0..@vms-1) {
  4064. my $value = simplify_number(convert_number($$values[$index][0]->value) / 1024);
  4065. $output .= $vms[$index]->name . " (" . $value . "MB), " if ($value > 0);
  4066. }
  4067. }
  4068. }
  4069. chop($output);
  4070. chop($output);
  4071. }
  4072. }
  4073. elsif ($subcommand eq "MEMCTL")
  4074. {
  4075. my $cluster_view;
  4076. ($cluster_view, $values) = return_cluster_performance_values($cluster, $defperfargs, 'mem', ('vmmemctl.average'));
  4077. if (defined($values))
  4078. {
  4079. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  4080. $np->add_perfdata(label => "mem_memctl", value => $value, uom => 'MB', threshold => $np->threshold);
  4081. $output = "memctl=" . $value . " MB: ";
  4082. $res = $np->check_threshold(check => $value);
  4083. if ($res != OK && $outputlist)
  4084. {
  4085. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $$cluster_view[0], properties => ['name', 'runtime.powerState']);
  4086. die "Runtime error\n" if (!defined($vm_views));
  4087. die "There are no VMs.\n" if (!@$vm_views);
  4088. my @vms = ();
  4089. foreach my $vm (@$vm_views)
  4090. {
  4091. push(@vms, $vm) if ($vm->get_property('runtime.powerState')->val eq "poweredOn");
  4092. }
  4093. $values = generic_performance_values(\@vms, $defperfargs, 'mem', ('vmmemctl.average'));
  4094. if (defined($values))
  4095. {
  4096. foreach my $index (0..@vms-1) {
  4097. my $value = simplify_number(convert_number($$values[$index][0]->value) / 1024);
  4098. $output .= $vms[$index]->name . " (" . $value . "MB), " if ($value > 0);
  4099. }
  4100. }
  4101. }
  4102. chop($output);
  4103. chop($output);
  4104. }
  4105. }
  4106. else
  4107. {
  4108. $res = CRITICAL;
  4109. $output = "CLUSTER MEM - unknown subcommand\n" . $np->opts->_help;
  4110. }
  4111. }
  4112. else
  4113. {
  4114. $values = return_cluster_performance_values($cluster, $defperfargs, 'mem', ('consumed.average', 'usage.average', 'overhead.average', 'swapused.average', 'vmmemctl.average'));
  4115. if (defined($values))
  4116. {
  4117. my $value1 = simplify_number(convert_number($$values[0][0]->value) / 1024);
  4118. my $value2 = simplify_number(convert_number($$values[0][1]->value) * 0.01);
  4119. my $value3 = simplify_number(convert_number($$values[0][2]->value) / 1024);
  4120. my $value4 = simplify_number(convert_number($$values[0][3]->value) / 1024);
  4121. my $value5 = simplify_number(convert_number($$values[0][4]->value) / 1024);
  4122. $np->add_perfdata(label => "mem_usagemb", value => $value1, uom => 'MB', threshold => $np->threshold);
  4123. $np->add_perfdata(label => "mem_usage", value => $value2, uom => '%', threshold => $np->threshold);
  4124. $np->add_perfdata(label => "mem_overhead", value => $value3, uom => 'MB', threshold => $np->threshold);
  4125. $np->add_perfdata(label => "mem_swap", value => $value4, uom => 'MB', threshold => $np->threshold);
  4126. $np->add_perfdata(label => "mem_memctl", value => $value5, uom => 'MB', threshold => $np->threshold);
  4127. $res = OK;
  4128. $output = "mem usage=" . $value1 . " MB (" . $value2 . "%), overhead=" . $value3 . " MB, swapped=" . $value4 . " MB, memctl=" . $value5 . " MB";
  4129. }
  4130. }
  4131. return ($res, $output);
  4132. }
  4133. sub cluster_cluster_info
  4134. {
  4135. my ($cluster, $np, $subcommand) = @_;
  4136. my $res = CRITICAL;
  4137. my $output = 'CLUSTER clusterServices Unknown error';
  4138. if (defined($subcommand))
  4139. {
  4140. if ($subcommand eq "EFFECTIVECPU")
  4141. {
  4142. $values = return_cluster_performance_values($cluster, $defperfargs, 'clusterServices', ('effectivecpu.average'));
  4143. if (defined($values))
  4144. {
  4145. my $value = simplify_number(convert_number($$values[0][0]->value) * 0.01);
  4146. $np->add_perfdata(label => "effective cpu", value => $value, uom => 'MHz', threshold => $np->threshold);
  4147. $output = "effective cpu=" . $value . " MHz";
  4148. $res = $np->check_threshold(check => $value);
  4149. }
  4150. }
  4151. elsif ($subcommand eq "EFFECTIVEMEM")
  4152. {
  4153. $values = return_cluster_performance_values($cluster, $defperfargs, 'clusterServices', ('effectivemem.average'));
  4154. if (defined($values))
  4155. {
  4156. my $value = simplify_number(convert_number($$values[0][0]->value) / 1024);
  4157. $np->add_perfdata(label => "effectivemem", value => $value, uom => 'MB', threshold => $np->threshold);
  4158. $output = "effective mem=" . $value . " MB";
  4159. $res = $np->check_threshold(check => $value);
  4160. }
  4161. }
  4162. elsif ($subcommand eq "FAILOVER")
  4163. {
  4164. $values = return_cluster_performance_values($cluster, $defperfargs, 'clusterServices', ('failover.latest:*'));
  4165. if (defined($values))
  4166. {
  4167. my $value = simplify_number(convert_number($$values[0][0]->value));
  4168. $np->add_perfdata(label => "failover", value => $value, threshold => $np->threshold);
  4169. $output = "failover=" . $value . " ";
  4170. $res = $np->check_threshold(check => $value);
  4171. }
  4172. }
  4173. elsif ($subcommand eq "CPUFAIRNESS")
  4174. {
  4175. $values = return_cluster_performance_values($cluster, $defperfargs, 'clusterServices', ('cpufairness.latest'));
  4176. if (defined($values))
  4177. {
  4178. my $value = simplify_number(convert_number($$values[0][0]->value));
  4179. $np->add_perfdata(label => "cpufairness", value => $value, uom => '%', threshold => $np->threshold);
  4180. $output = "cpufairness=" . $value . " %";
  4181. $res = $np->check_threshold(check => $value);
  4182. }
  4183. }
  4184. elsif ($subcommand eq "MEMFAIRNESS")
  4185. {
  4186. $values = return_cluster_performance_values($cluster, $defperfargs, 'clusterServices', ('memfairness.latest'));
  4187. if (defined($values))
  4188. {
  4189. my $value = simplify_number((convert_number($$values[0][0]->value)));
  4190. $np->add_perfdata(label => "memfairness", value => $value, uom => '%', threshold => $np->threshold);
  4191. $output = "memfairness=" . $value . " %";
  4192. $res = $np->check_threshold(check => $value);
  4193. }
  4194. }
  4195. else
  4196. {
  4197. $res = CRITICAL;
  4198. $output = "CLUSTER clusterServices - unknown subcommand\n" . $np->opts->_help;
  4199. }
  4200. }
  4201. else
  4202. {
  4203. $values = return_cluster_performance_values($cluster, $defperfargs, 'clusterServices', ('effectivecpu.average', 'effectivemem.average'));
  4204. if (defined($values))
  4205. {
  4206. my $value1 = simplify_number(convert_number($$values[0][0]->value));
  4207. my $value2 = simplify_number(convert_number($$values[0][1]->value) / 1024);
  4208. $np->add_perfdata(label => "effective cpu", value => $value1, uom => 'MHz', threshold => $np->threshold);
  4209. $np->add_perfdata(label => "effective mem", value => $value2, uom => 'MB', threshold => $np->threshold);
  4210. $res = OK;
  4211. $output = "effective cpu=" . $value1 . " MHz, effective Mem=" . $value2 . " MB";
  4212. }
  4213. }
  4214. return ($res, $output);
  4215. }
  4216. sub cluster_runtime_info
  4217. {
  4218. my ($cluster, $np, $subcommand, $blacklist) = @_;
  4219. my $res = CRITICAL;
  4220. my $output = 'CLUSTER RUNTIME Unknown error';
  4221. my $runtime;
  4222. my $cluster_view = Vim::find_entity_view(view_type => 'ClusterComputeResource', filter => { name => "$cluster" }, properties => ['name', 'overallStatus', 'configIssue']);
  4223. die "Cluster \"" . $$cluster{"name"} . "\" does not exist\n" if (!defined($cluster_view));
  4224. $cluster_view->update_view_data();
  4225. if (defined($subcommand))
  4226. {
  4227. if (($subcommand eq "LIST") || ($subcommand eq "LISTVM"))
  4228. {
  4229. my %vm_state_strings = ("poweredOn" => "UP", "poweredOff" => "DOWN", "suspended" => "SUSPENDED");
  4230. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $cluster_view, properties => ['name', 'runtime']);
  4231. die "Runtime error\n" if (!defined($vm_views));
  4232. die "There are no VMs.\n" if (!defined($vm_views));
  4233. my $up = 0;
  4234. $output = '';
  4235. foreach my $vm (@$vm_views)
  4236. {
  4237. my $vm_state = $vm_state_strings{$vm->runtime->powerState->val};
  4238. if ($vm_state eq "UP")
  4239. {
  4240. $up++;
  4241. $output .= $vm->name . "(OK), ";
  4242. }
  4243. else
  4244. {
  4245. $output = $vm->name . "(" . $vm_state . "), " . $output;
  4246. }
  4247. }
  4248. chop($output);
  4249. chop($output);
  4250. $res = OK;
  4251. $output = $up . "/" . @$vm_views . " VMs up: " . $output;
  4252. $np->add_perfdata(label => "vmcount", value => $up, uom => 'units', threshold => $np->threshold);
  4253. $res = $np->check_threshold(check => $up) if (defined($np->threshold));
  4254. }
  4255. elsif ($subcommand eq "LISTHOST")
  4256. {
  4257. my %host_state_strings = ("poweredOn" => "UP", "poweredOff" => "DOWN", "suspended" => "SUSPENDED", "standBy" => "STANDBY", "MaintenanceMode" => "Maintenance Mode");
  4258. my $host_views = Vim::find_entity_views(view_type => 'HostSystem', begin_entity => $cluster_view, properties => ['name', 'runtime.powerState']);
  4259. die "Runtime error\n" if (!defined($host_views));
  4260. die "There are no hosts.\n" if (!defined($host_views));
  4261. my $up = 0;
  4262. my $unknown = 0;
  4263. $output = '';
  4264. foreach my $host (@$host_views) {
  4265. $host->update_view_data(['name', 'runtime.powerState']);
  4266. my $host_state = $host_state_strings{$host->get_property('runtime.powerState')->val};
  4267. $unknown += $host_state eq "UNKNOWN";
  4268. if ($host_state eq "UP" ) {
  4269. $up++;
  4270. $output .= $host->name . "(UP), ";
  4271. }
  4272. else {
  4273. $output = $host->name . "(" . $host_state . "), " . $output;
  4274. }
  4275. }
  4276. chop($output);
  4277. chop($output);
  4278. $res = OK;
  4279. $output = $up . "/" . @$host_views . " Hosts up: " . $output;
  4280. $np->add_perfdata(label => "vmcount", value => $up, uom => 'units', threshold => $np->threshold);
  4281. $res = $np->check_threshold(check => $up) if (defined($np->threshold));
  4282. $res = UNKNOWN if ($res == OK && $unknown);
  4283. }
  4284. elsif ($subcommand eq "STATUS")
  4285. {
  4286. if (defined($cluster_view->overallStatus))
  4287. {
  4288. my $status = $cluster_view->overallStatus->val;
  4289. $output = "overall status=" . $status;
  4290. $res = check_health_state($status);
  4291. }
  4292. else
  4293. {
  4294. $output = "Insufficient rights to access status info on the DC\n";
  4295. $res = WARNING;
  4296. }
  4297. }
  4298. elsif ($subcommand eq "ISSUES")
  4299. {
  4300. my $issues = $cluster_view->configIssue;
  4301. my $issues_count = 0;
  4302. $output = '';
  4303. if (defined($issues))
  4304. {
  4305. foreach (@$issues)
  4306. {
  4307. if (defined($blacklist))
  4308. {
  4309. my $name = ref($_);
  4310. next if ($blacklist =~ m/(^|\s|\t|,)\Q$name\E($|\s|\t|,)/);
  4311. }
  4312. $output .= format_issue($_) . "; ";
  4313. $issues_count++;
  4314. }
  4315. }
  4316. if ($output eq '')
  4317. {
  4318. $res = OK;
  4319. $output = 'No config issues';
  4320. }
  4321. $np->add_perfdata(label => "issues", value => $issues_count);
  4322. }
  4323. else
  4324. {
  4325. $res = CRITICAL;
  4326. $output = "CLUSTER RUNTIME - unknown subcommand\n" . $np->opts->_help;
  4327. }
  4328. }
  4329. else
  4330. {
  4331. my %cluster_maintenance_state = (0 => "no", 1 => "yes");
  4332. my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $cluster_view, properties => ['name', 'runtime.powerState']);
  4333. my $up = 0;
  4334. if (defined($vm_views))
  4335. {
  4336. foreach my $vm (@$vm_views) {
  4337. $up += $vm->get_property('runtime.powerState')->val eq "poweredOn";
  4338. }
  4339. $np->add_perfdata(label => "vmcount", value => $up, uom => 'units', threshold => $np->threshold);
  4340. $output = $up . "/" . @$vm_views . " VMs up";
  4341. }
  4342. else
  4343. {
  4344. $output = "No VMs installed";
  4345. }
  4346. my $AlertCount = 0;
  4347. my $SensorCount = 0;
  4348. my ($cpuStatusInfo, $storageStatusInfo, $memoryStatusInfo, $numericSensorInfo);
  4349. $res = OK;
  4350. if (defined($cluster_view->overallStatus))
  4351. {
  4352. my $overallstatus = $cluster_view->overallStatus->val;
  4353. $res = check_health_state($overallstatus);
  4354. $output .= ", overall status=" . $overallstatus . ", ";
  4355. }
  4356. my $issues = $cluster_view->configIssue;
  4357. if (defined($issues))
  4358. {
  4359. $output .= @$issues . " config issue(s)";
  4360. $res = WARNING if ($res == OK);
  4361. }
  4362. else
  4363. {
  4364. $output .= "no config issues";
  4365. }
  4366. }
  4367. return ($res, $output);
  4368. }
  4369. sub cluster_list_vm_volumes_info
  4370. {
  4371. my ($cluster, $np, $subcommand, $blacklist, $perc, $addopts) = @_;
  4372. my $cluster_view = Vim::find_entity_view(view_type => 'ClusterComputeResource', filter => {name => "$cluster"}, properties => ['name', 'datastore']);
  4373. die "Insufficient rights to access Datastores on the Host\n" if (!defined($cluster_view->datastore));
  4374. return datastore_volumes_info($cluster_view->datastore, $np, $subcommand, $blacklist, $perc, $addopts);
  4375. }