Cacti WMI 2 – Creating A Perl WMI Script

Cacti WMI 2 – Creating A Perl WMI Script

Creating A Perl WMI Script

This article explains how to create a basic Perl script to monitor the CPU Load of a sngle CPU Windows server through WMI.

The script is by no means approved by Cacti and is written to provide a very basic output for a single CPU Windows server.

For further information a copy of the script is attached to this article.

Configuring The Input Arguments

In order to allow Cacti to supply the credentials and host name to the script we first need to configured the input arguments which will be used.  To configure the input arguments for the script perform the following steps :-

  • Create the file /var/www/cacti/scripts/wni-cpu-load.pl
  • Edit the file and enter in the lines shown below :-

#!/usr/bin/perl

use strict;

  • The two lines above configure the environment for the script and enforce strict usage of variables within the script.
  • Next to configure the input arguments for the credentials and host name add the following lines :-

my $user = $ARGV[0];

my $password = $ARGV[1];

my $server = $ARGV[2];

The lines added above have now configured the perl script to expect the username as the first argument, the password as the second, and the host name as the third.

Constructing The WMI Command

The next step in the script is to construct the WMI command which will be executed to retrieve the WMI results from the Windows server.  To construct the WMI command perform the following sections.

Testing The WMI Command

The WMI command to be used in the script can be tested from the command line of the server before adding to the script in order to confirm that it is working correctly.  To test the WMI command perform the following steps :-

  • From the command line execute the following command :-

wmic -U {Windows Domain}/{WMI User}%”{Password}” //{Server Name} “select DeviceID,LoadPercentage from Win32_Processor”

E.G. – wmic -U MyDomain/administrator%”Password” //10.20.30.40″select DeviceID,LoadPercentage from Win32_Processor”

  • If the command works correctly it should return the lines below :-

CLASS: Win32_Processor

DeviceID|LoadPercentage

CPU0|0

N.B. The LoadPercentage figure in line 3 may differ on your system

Adding The Command To The Script

  • Following on from the input arguments in the previous section continue editing the script
  • To create the WMI command line add the following line to the script

my $result = `wmic -U {Windows Domain}/$user%”$password” //$server “select DeviceID,LoadPercentage from Win32_Processor”`;

N.B Change the {Windows Domain} to your own domain name

E.G. – my $result = `wmic -U MyDomain/$user%”$password”…..

  • The line above will pass the arguments provided to the script to the WMI command and select the DeviceID and LoadPercentage from the Win32_Processor WMI Class.

Manipulating The Results

The results provided from the WMI command now need to be manipulated so that we can strip out only the LoadPercentage from the last line which we require.

To manipulate the results from the WMI command perform the following steps :-

  • Following on from the adding the WMI command to the script add the following lines

my @results=split(/\n/,$result);

my @tmparray=split(/\|/, @results[2]);

  • The first line above splits the results in to an array by the line return (\n) which will make an array of 3 elements.
  • The second line above splits the 2nd element (The last line of the results) by the pipe (|) symbol so that we can retrieve the LoadPercentage value

Displaying The Results

The final part of the script is to display the results in a format which Cacti can understand.  When displaying multiple results from a script to Cacti the results need to formatted in the way below :-

{OutputField1}:{Result} {OutputField2}:{Result}

E.G. – CPUNo:{Result} Load:{Result}

The Output Fields will be used later in the Data Input Method creation part of this installation and must be formatted correctly in order for it to pick up the correct values.

To display the results from the WMI command perform the following steps :-

  • Following on from the results manipulation lines above add the following line to the script

print “CPUNo:@tmparray[0] Load:@tmparray[1]\n”;

  • The line above will output the following when the script is executed :-

CPUNo:0 Load:0

  • Save and Exit the script

The Whole Script

Below is how the script should look once saved :-

#!/usr/bin/perl

use strict;

my $user = $ARGV[0];

my $password = $ARGV[1];

my $server = $ARGV[2];

my $result = `wmic -U wonderland/$user%”$password” //$server “select DeviceID,LoadPercentage from Win32_Processor”`;

my @results=split(/\n/,$result);

my @tmparray=split(/\|/, @results[2]);

print “CPUNo:@tmparray[0] Load:@tmparray[1]\n”;

Testing The Script

To test that the script is working correctly perform the following steps :-

  • In the command prompt execute the following command :-

perl wmi-cpu-load.pl {username} {password} {server name}

E.G. – perl wmi-cpu-load.pl administrator Password 10.20.30.40

  • If the script working and the credentials have the appropriate access to WMI on the server it should return the following :-

CPUNo:0 Load:1

  • Obviously the Load value will differ on your system depending on how busy the server is