| 2 | 
           rodolico | 
           1 | 
           #! /usr/bin/perl -w
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           # sysinfo
  | 
        
        
            | 
            | 
           4 | 
           # Author: R. W. Rodolico
  | 
        
        
            | 
            | 
           5 | 
           # Primary client portion of sysinfo system. Will collect information about its current
  | 
        
        
            | 
            | 
           6 | 
           # host and create a report containing the information. This report can then be processed
  | 
        
        
            | 
            | 
           7 | 
           # by process_sysinfo.pl on the collection computer.
  | 
        
        
            | 
            | 
           8 | 
           # output file consists of an XML file of the form:
  | 
        
        
            | 
            | 
           9 | 
           #  <sysinfo3.0.0>
  | 
        
        
            | 
            | 
           10 | 
           #    <diskinfo name='/dev/xvda3'>
  | 
        
        
            | 
            | 
           11 | 
           #      <fstype>ext3</fstype>
  | 
        
        
            | 
            | 
           12 | 
           #      <mount>/home</mount>
  | 
        
        
            | 
            | 
           13 | 
           #      <size>51606140</size>
  | 
        
        
            | 
            | 
           14 | 
           #      <used>331472</used>
  | 
        
        
            | 
            | 
           15 | 
           #    </diskinfo>
  | 
        
        
            | 
            | 
           16 | 
           #    <network name='eth0'>
  | 
        
        
            | 
            | 
           17 | 
           #      <address>192.168.1.3</address>
  | 
        
        
            | 
            | 
           18 | 
           #      <ip6address>fe80::216:3eff:fefb:4e10</ip6address>
  | 
        
        
            | 
            | 
           19 | 
           #      <ip6networkbits>64</ip6networkbits>
  | 
        
        
            | 
            | 
           20 | 
           #      <mac>00:16:3e:fb:4e:10</mac>
  | 
        
        
            | 
            | 
           21 | 
           #      <mtu>1500</mtu>
  | 
        
        
            | 
            | 
           22 | 
           #      <netmask>255.255.255.0</netmask>
  | 
        
        
            | 
            | 
           23 | 
           #    </network>
  | 
        
        
            | 
            | 
           24 | 
           #    <operatingsystem>
  | 
        
        
            | 
            | 
           25 | 
           #      <codename>squeeze</codename>
  | 
        
        
            | 
            | 
           26 | 
           #      <description>Debian GNU/Linux 6.0.4 (squeeze)</description>
  | 
        
        
            | 
            | 
           27 | 
           #      <distribution>Debian</distribution>
  | 
        
        
            | 
            | 
           28 | 
           #      <kernel>2.6.32-5-xen-686</kernel>
  | 
        
        
            | 
            | 
           29 | 
           #      <os_name>Linux</os_name>
  | 
        
        
            | 
            | 
           30 | 
           #      <os_version>Linux version 2.6.32-5-xen-686 (Debian 2.6.32-41) (ben@decadent.org.uk) (gcc version 4.3.5 (Debian 4.3.5-4) ) #1 SMP Mon Jan 16 19:46:09 UTC 2012</os_version>
  | 
        
        
            | 
            | 
           31 | 
           #      <release>6.0.4</release>
  | 
        
        
            | 
            | 
           32 | 
           #    </operatingsystem>
  | 
        
        
            | 
            | 
           33 | 
           #    <pci name='0000:00:00.0'>
  | 
        
        
            | 
            | 
           34 | 
           #      <class>RAM memory</class>
  | 
        
        
            | 
            | 
           35 | 
           #      <device>MCP55 Memory Controller</device>
  | 
        
        
            | 
            | 
           36 | 
           #      <rev>a2</rev>
  | 
        
        
            | 
            | 
           37 | 
           #      <sdevice>Device cb84</sdevice>
  | 
        
        
            | 
            | 
           38 | 
           #      <slot>0000:00:00.0</slot>
  | 
        
        
            | 
            | 
           39 | 
           #      <svendor>nVidia Corporation</svendor>
  | 
        
        
            | 
            | 
           40 | 
           #      <vendor>nVidia Corporation</vendor>
  | 
        
        
            | 
            | 
           41 | 
           #    </pci>
  | 
        
        
            | 
            | 
           42 | 
           #    <report>
  | 
        
        
            | 
            | 
           43 | 
           #      <client>Staffmasters</client>
  | 
        
        
            | 
            | 
           44 | 
           #      <date>2012-05-01 03:00</date>
  | 
        
        
            | 
            | 
           45 | 
           #      <version>2.0.0</version>
  | 
        
        
            | 
            | 
           46 | 
           #    </report>
  | 
        
        
            | 
            | 
           47 | 
           #    <software name='aptitude'>
  | 
        
        
            | 
            | 
           48 | 
           #      <description>terminal-based package manager (terminal interface only)</description>
  | 
        
        
            | 
            | 
           49 | 
           #      <version>0.6.3-3.2+squeeze1</version>
  | 
        
        
            | 
            | 
           50 | 
           #    </software>
  | 
        
        
            | 
            | 
           51 | 
           #    <system>
  | 
        
        
            | 
            | 
           52 | 
           #      <cpu_speed>1800.103</cpu_speed>
  | 
        
        
            | 
            | 
           53 | 
           #      <cpu_sub>i686</cpu_sub>
  | 
        
        
            | 
            | 
           54 | 
           #      <cpu_type>GenuineIntel</cpu_type>
  | 
        
        
            | 
            | 
           55 | 
           #      <hostname>backup.staffmasters.local</hostname>
  | 
        
        
            | 
            | 
           56 | 
           #      <last_boot>1333259809</last_boot>
  | 
        
        
            | 
            | 
           57 | 
           #      <memory>520852</memory>
  | 
        
        
            | 
            | 
           58 | 
           #      <num_cpu>1</num_cpu>
  | 
        
        
            | 
            | 
           59 | 
           #    </system>
  | 
        
        
            | 
            | 
           60 | 
           #  </sysinfo3.0.0>
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
           #
  | 
        
        
            | 
            | 
           64 | 
           # Version 1.3 20071104
  | 
        
        
            | 
            | 
           65 | 
           # added capability of e-mailing the results by itself and external configuration file
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
           # Version 1.3.1 20071110
  | 
        
        
            | 
            | 
           68 | 
           # added du -sk to explicitly do directory sizes in 'k'. Also, fixed some documentation
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
           # Version 1.3.3 20081104
  | 
        
        
            | 
            | 
           71 | 
           # modified hostname to hostname -f, and allowed user to place custom value in configuration file
  | 
        
        
            | 
            | 
           72 | 
           # also, modified to go with Debian standards in preparation to creating a debian package.
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
           # Version 2.0 20081208
  | 
        
        
            | 
            | 
           75 | 
           # Modified to use different libraries for different OS's in preparation to porting to Windows
  | 
        
        
            | 
            | 
           76 | 
           # Uses different packages based on which OS it is on.
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
           # Version 3.0 20120923
  | 
        
        
            | 
            | 
           79 | 
           # Major revision. Most internal intelligence pulled out and put into modules and data transfer format has been changed to YAML
  | 
        
        
            | 
            | 
           80 | 
           #
  | 
        
        
            | 
            | 
           81 | 
           # Base system only pulls client name, machine name and machine number, all of which can be set in the configuration file
  | 
        
        
            | 
            | 
           82 | 
           # if the value is not set, it attempts various means to determine the values and, if it fails, aborts with an error message
  | 
        
        
            | 
            | 
           83 | 
           #    client name -- REQUIRED, must come from configuration file
  | 
        
        
            | 
            | 
           84 | 
           #    machine name --  REQUIRED, if not set via conf file, attempts hostname command (hostname -f) or module getHostName
  | 
        
        
            | 
            | 
           85 | 
           #    machine number -- REQUIRED, if not set via conf file, attempts "echo `hostname -f`-clientname | md5sum" or module getSerial
  | 
        
        
            | 
            | 
           86 | 
           # modules are stored in "configuration directory/modules" (/etc/sysinfo/modules on most Linux systems) and are processed in 
  | 
        
        
            | 
            | 
           87 | 
           # standard sort order (case sensitive). 
  | 
        
        
            | 
            | 
           88 | 
           # Module filenames may contain alpha-numeric, underscore and the period only (files containing other characters are ignored).
  | 
        
        
            | 
            | 
           89 | 
           # Modules should set their exit code to 0 for success, and non-zero for failure
  | 
        
        
            | 
            | 
           90 | 
           # Modules should return 0 or more tab delimited, newline terminated strings, processed as one record per line
  | 
        
        
            | 
            | 
           91 | 
           # A module return string line is processed as follows:
  | 
        
        
            | 
            | 
           92 | 
           #     category \t [category \t ...] \t key \t value
  | 
        
        
            | 
            | 
           93 | 
           # example:
  | 
        
        
            | 
            | 
           94 | 
           #    System \t num_cpu \t 1
  | 
        
        
            | 
            | 
           95 | 
           #    System \t Users \t root \t /root/
  | 
        
        
            | 
            | 
           96 | 
           # (note, if non-zero exit code returned, return value is assumed to be error message and is printed to STDERR) 
  | 
        
        
            | 
            | 
           97 | 
           # sysinfo stores the result in a hash, using categories as the keys (case sensitive), thus, the above results in
  | 
        
        
            | 
            | 
           98 | 
           # $store{'System'}{'num_cpu'} = '1';
  | 
        
        
            | 
            | 
           99 | 
           # $store{'System'}{'Users'}{'root'} = '/root';
  | 
        
        
            | 
            | 
           100 | 
           # upon completion, sysinfo converts the $store hash into an XML or YAML string for transfer
  | 
        
        
            | 
            | 
           101 | 
           # It then sends it to the main server as defined in the conf file.
  | 
        
        
            | 
            | 
           102 | 
           # NOTE: YAML is hand crafted to kill any requirements for external libraries
  | 
        
        
            | 
            | 
           103 | 
           # see sub hashToYAML for details
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
           # Following are global variables overridden if configuration file exists
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
           my $TESTING = 0; # level's 0 (none) to 3 defined and increase verbosity while decreasing functionality
  | 
        
        
            | 
            | 
           108 | 
           my $indentLevel = 2; # number of spaces to indent per level in XML or YAML
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
            | 
            | 
           110 | 
           $indentLevel = 3 if $TESTING;
  | 
        
        
            | 
            | 
           111 | 
           if ($TESTING) {
  | 
        
        
            | 
            | 
           112 | 
              use Data::Dumper;
  | 
        
        
            | 
            | 
           113 | 
           }
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
           my $configurationDir = $TESTING ? './' : '/etc/sysinfo/'; # use same directory as app if TESTING
  | 
        
        
            | 
            | 
           117 | 
           my $configurationFile = $configurationDir . 'sysinfo.conf'; # build full path to conf file
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
           # default values may be overridden in conf file
  | 
        
        
            | 
            | 
           120 | 
           my $moduleDir = $configurationDir . 'modules/'; # modules are in a subdirectory of the conf directory
  | 
        
        
            | 
            | 
           121 | 
           my $scriptsDir = $configurationDir . 'scripts/'; # as are scripts
  | 
        
        
            | 
            | 
           122 | 
           my $hostname = &getHostName; # set hostname to default to hostname -f, unless overridden in conf file
  | 
        
        
            | 
            | 
           123 | 
           my $reportDate = &getReportDate; # set report date
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
           # defined only in configuration file
  | 
        
        
            | 
            | 
           126 | 
           my $iSendReports; # hash which can optionally use a script to send the report to the server
  | 
        
        
            | 
            | 
           127 | 
           my $clientName; # Required!! Must be set in conf file (no defaults)
  | 
        
        
            | 
            | 
           128 | 
           my $serialNumber; # defined in configuration file, or later in here if not defined there
  | 
        
        
            | 
            | 
           129 | 
              | 
        
        
            | 
            | 
           130 | 
           my $VERSION = '3.0.0'; # used in sending the data file. sets version of XML/YAML data file
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
           #######################################################
  | 
        
        
            | 
            | 
           133 | 
           #
  | 
        
        
            | 
            | 
           134 | 
           # loadConfigurationFile($confFile)
  | 
        
        
            | 
            | 
           135 | 
           #
  | 
        
        
            | 
            | 
           136 | 
           # Loads configuration file defined by $configurationFile, and dies if not available
  | 
        
        
            | 
            | 
           137 | 
           # Reads entire contents into memory where it is eval'd in main program
  | 
        
        
            | 
            | 
           138 | 
           # Parameters: configuration file fully path/file name
  | 
        
        
            | 
            | 
           139 | 
           # NOTE: conf file must be a valid Perl file
  | 
        
        
            | 
            | 
           140 | 
           #
  | 
        
        
            | 
            | 
           141 | 
           #######################################################
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
           sub loadConfigurationFile {
  | 
        
        
            | 
            | 
           144 | 
              my $confFile = shift;
  | 
        
        
            | 
            | 
           145 | 
              open CONFFILE, "<$confFile" or die "Can not open configuration file $confFile: $!\n";
  | 
        
        
            | 
            | 
           146 | 
              my $confFileContents = join( '', <CONFFILE> ); # just slurp it into memory
  | 
        
        
            | 
            | 
           147 | 
              close CONFFILE;
  | 
        
        
            | 
            | 
           148 | 
              return ($confFileContents);
  | 
        
        
            | 
            | 
           149 | 
           }
  | 
        
        
            | 
            | 
           150 | 
              | 
        
        
            | 
            | 
           151 | 
           #######################################################
  | 
        
        
            | 
            | 
           152 | 
           #
  | 
        
        
            | 
            | 
           153 | 
           # sendResults( $parameters, $message, $scriptDirectory )
  | 
        
        
            | 
            | 
           154 | 
           #
  | 
        
        
            | 
            | 
           155 | 
           # Sends results of run to server using external script. If external
  | 
        
        
            | 
            | 
           156 | 
           # script not defined, just print to STDOUT
  | 
        
        
            | 
            | 
           157 | 
           #
  | 
        
        
            | 
            | 
           158 | 
           # Parameters
  | 
        
        
            | 
            | 
           159 | 
           #  $parameters - a hash containing the information necessary to make the transfer
  | 
        
        
            | 
            | 
           160 | 
           #  $message - the message to be sent
  | 
        
        
            | 
            | 
           161 | 
           #  $scriptDirectory - path (not filename) of script to be executed
  | 
        
        
            | 
            | 
           162 | 
           # 
  | 
        
        
            | 
            | 
           163 | 
           # $parameters contains different key/value pairs depending on the script used
  | 
        
        
            | 
            | 
           164 | 
           #             for example, a stand-alone SMTP script may need a username/password,
  | 
        
        
            | 
            | 
           165 | 
           #             smtp server name, port number, from and to address
  | 
        
        
            | 
            | 
           166 | 
           #             while an http transfer may only need a script name
  | 
        
        
            | 
            | 
           167 | 
           #             See the individual scripts to determine what parameters need to be
  | 
        
        
            | 
            | 
           168 | 
           #             filled in.
  | 
        
        
            | 
            | 
           169 | 
           #             The only required parameter is 'sendScript' which must contain the
  | 
        
        
            | 
            | 
           170 | 
           #             name of the script to execute (and it must be located in $scriptDirectory)
  | 
        
        
            | 
            | 
           171 | 
           # SCRIPT must contain one sub named doit, that accepts three parameters, the hash, 
  | 
        
        
            | 
            | 
           172 | 
           #       the message, and, optionally, the script directory
  | 
        
        
            | 
            | 
           173 | 
           #
  | 
        
        
            | 
            | 
           174 | 
           # If script not defined, just dump to STDOUT. With a properly set up cron job, the output
  | 
        
        
            | 
            | 
           175 | 
           # would then be sent via e-mail to an administrative account, possibly root
  | 
        
        
            | 
            | 
           176 | 
           #
  | 
        
        
            | 
            | 
           177 | 
           #######################################################
  | 
        
        
            | 
            | 
           178 | 
           sub sendResults {
  | 
        
        
            | 
            | 
           179 | 
              my ( $parameters, $message, $scriptDirectory ) = @_;
  | 
        
        
            | 
            | 
           180 | 
              if ( $$parameters{'sendScript'} ) { # they have a script they want to used
  | 
        
        
            | 
            | 
           181 | 
                 # load the chosen script into memory
  | 
        
        
            | 
            | 
           182 | 
                 require $scriptDirectory . $$parameters{'sendScript'};
  | 
        
        
            | 
            | 
           183 | 
                 # execute the "doit" sub from that script
  | 
        
        
            | 
            | 
           184 | 
                 &doit( $parameters, $message, $scriptDirectory );
  | 
        
        
            | 
            | 
           185 | 
              } else { # no script, just dump to STDOUT
  | 
        
        
            | 
            | 
           186 | 
                 print $message;
  | 
        
        
            | 
            | 
           187 | 
              }
  | 
        
        
            | 
            | 
           188 | 
           }
  | 
        
        
            | 
            | 
           189 | 
              | 
        
        
            | 
            | 
           190 | 
           #######################################################
  | 
        
        
            | 
            | 
           191 | 
           #
  | 
        
        
            | 
            | 
           192 | 
           # getReportDate
  | 
        
        
            | 
            | 
           193 | 
           #
  | 
        
        
            | 
            | 
           194 | 
           # return current system date as YYYY-MM-DD HH:MM:SS
  | 
        
        
            | 
            | 
           195 | 
           #
  | 
        
        
            | 
            | 
           196 | 
           #######################################################
  | 
        
        
            | 
            | 
           197 | 
           sub getReportDate {
  | 
        
        
            | 
            | 
           198 | 
              ($second, $minute, $hour, $dayOfMonth, $month, $year) = localtime();
  | 
        
        
            | 
            | 
           199 | 
              return sprintf( "%4u-%02u-%02u %02u:%02u:%02u", $year+1900, $month+1, $dayOfMonth, $hour, $minute, $second );
  | 
        
        
            | 
            | 
           200 | 
           }
  | 
        
        
            | 
            | 
           201 | 
              | 
        
        
            | 
            | 
           202 | 
           #######################################################
  | 
        
        
            | 
            | 
           203 | 
           #
  | 
        
        
            | 
            | 
           204 | 
           # getHostName
  | 
        
        
            | 
            | 
           205 | 
           #
  | 
        
        
            | 
            | 
           206 | 
           # return hostname from hostname -f
  | 
        
        
            | 
            | 
           207 | 
           #
  | 
        
        
            | 
            | 
           208 | 
           #######################################################
  | 
        
        
            | 
            | 
           209 | 
           sub getHostName {
  | 
        
        
            | 
            | 
           210 | 
              $hostname = `hostname -f`;
  | 
        
        
            | 
            | 
           211 | 
              chomp $hostname;
  | 
        
        
            | 
            | 
           212 | 
              return $hostname;
  | 
        
        
            | 
            | 
           213 | 
           }
  | 
        
        
            | 
            | 
           214 | 
              | 
        
        
            | 
            | 
           215 | 
           #######################################################
  | 
        
        
            | 
            | 
           216 | 
           #
  | 
        
        
            | 
            | 
           217 | 
           # getSerialNumber
  | 
        
        
            | 
            | 
           218 | 
           #
  | 
        
        
            | 
            | 
           219 | 
           # Calculates a serial number by creating an md5sum of 
  | 
        
        
            | 
            | 
           220 | 
           # the client name, a space, a dash, a space, and the hostname
  | 
        
        
            | 
            | 
           221 | 
           #
  | 
        
        
            | 
            | 
           222 | 
           #######################################################
  | 
        
        
            | 
            | 
           223 | 
           sub getSerialNumber {
  | 
        
        
            | 
            | 
           224 | 
              my $hash = `echo $clientName-$hostname | md5sum`;
  | 
        
        
            | 
            | 
           225 | 
              $hash =~ m/^([a-z0-9]+)/;
  | 
        
        
            | 
            | 
           226 | 
              return $1;
  | 
        
        
            | 
            | 
           227 | 
           }
  | 
        
        
            | 
            | 
           228 | 
              | 
        
        
            | 
            | 
           229 | 
              | 
        
        
            | 
            | 
           230 | 
           sub escapeForYAML {
  | 
        
        
            | 
            | 
           231 | 
              my $value = shift;
  | 
        
        
            | 
            | 
           232 | 
              $value =~ s/'/\\'/gi; # escape single quotes
  | 
        
        
            | 
            | 
           233 | 
              $value =~ s/"/\\"/gi; # escape double quotes
  | 
        
        
            | 
            | 
           234 | 
              # pound sign indicates start of a comment and thus loses part
  | 
        
        
            | 
            | 
           235 | 
              # of strings. Surrounding it by double quotes in next statement
  | 
        
        
            | 
            | 
           236 | 
              # allows 
  | 
        
        
            | 
            | 
           237 | 
              $value = '"' . $value . '"' if ( $value =~ m/[#:]/ );
  | 
        
        
            | 
            | 
           238 | 
              return $value;
  | 
        
        
            | 
            | 
           239 | 
           }
  | 
        
        
            | 
            | 
           240 | 
              | 
        
        
            | 
            | 
           241 | 
           #######################################################
  | 
        
        
            | 
            | 
           242 | 
           #
  | 
        
        
            | 
            | 
           243 | 
           # hashToYAML( $hashRef, $indent )
  | 
        
        
            | 
            | 
           244 | 
           #
  | 
        
        
            | 
            | 
           245 | 
           # Converts a hash to a YAML string
  | 
        
        
            | 
            | 
           246 | 
           #
  | 
        
        
            | 
            | 
           247 | 
           # NOTE: This routine recursively calls itself for every level
  | 
        
        
            | 
            | 
           248 | 
           #       in the hash
  | 
        
        
            | 
            | 
           249 | 
           #
  | 
        
        
            | 
            | 
           250 | 
           # Parameters
  | 
        
        
            | 
            | 
           251 | 
           #     $hashref - reference (address) of a hash
  | 
        
        
            | 
            | 
           252 | 
           #     $indent  - current indent level, defaults to 0
  | 
        
        
            | 
            | 
           253 | 
           #
  | 
        
        
            | 
            | 
           254 | 
           # Even though there are some very good libraries that do this
  | 
        
        
            | 
            | 
           255 | 
           # I chose to hand-code it so sysinfo can be run with no libraries
  | 
        
        
            | 
            | 
           256 | 
           # loaded. I chose to NOT do a full implementation, so special chars
  | 
        
        
            | 
            | 
           257 | 
           # that would normally be escaped are not in here. 
  | 
        
        
            | 
            | 
           258 | 
           # However, I followed all the RFC for the values that were given, so
  | 
        
        
            | 
            | 
           259 | 
           # assume any YAML reader can parse this
  | 
        
        
            | 
            | 
           260 | 
           # NOTE: YAML appears to give a resulting file 1/3 smaller than the above
  | 
        
        
            | 
            | 
           261 | 
           #       XML, and compresses down in like manner
  | 
        
        
            | 
            | 
           262 | 
           #
  | 
        
        
            | 
            | 
           263 | 
           #######################################################
  | 
        
        
            | 
            | 
           264 | 
           sub hashToYAML {
  | 
        
        
            | 
            | 
           265 | 
              my ($hashRef, $indent) = @_;
  | 
        
        
            | 
            | 
           266 | 
              $indent = 0 unless $indent; # default to 0 if not defined
  | 
        
        
            | 
            | 
           267 | 
              | 
        
        
            | 
            | 
           268 | 
              my $output; # where the output is stored
  | 
        
        
            | 
            | 
           269 | 
              foreach my $key ( keys %$hashRef ) { # for each key in the current reference
  | 
        
        
            | 
            | 
           270 | 
                 print "Looking at $key\n" if $TESTING > 3;
  | 
        
        
            | 
            | 
           271 | 
                 # see http://www.perlmonks.org/?node_id=175651 for isa function
  | 
        
        
            | 
            | 
           272 | 
                 if ( UNIVERSAL::isa( $$hashRef{$key}, 'HASH' ) ) { # is the value another hash?
  | 
        
        
            | 
            | 
           273 | 
                       # NOTE: unlike xml, indentation is NOT optional in YAML, so the following line verifies $indentlevel is non-zero
  | 
        
        
            | 
            | 
           274 | 
                       #       and, if it is, uses a default 3 character indentation
  | 
        
        
            | 
            | 
           275 | 
                       $output .= (' ' x $indent ) . &escapeForYAML($key) . ":\n" . # key, plus colon, plus newline
  | 
        
        
            | 
            | 
           276 | 
                               &hashToYAML( $$hashRef{$key}, $indent+($indentLevel ? $indentLevel : 3) ) . # add results of recursive call
  | 
        
        
            | 
            | 
           277 | 
                               "\n";
  | 
        
        
            | 
            | 
           278 | 
                 } elsif ( UNIVERSAL::isa( $$hashRef{$key}, 'ARRAY' ) ) { # is it an array? ignore it
  | 
        
        
            | 
            | 
           279 | 
                 } else { # it is a scalar, so just do <key>value</key>
  | 
        
        
            | 
            | 
           280 | 
                    $output .= (' ' x $indent ) . &escapeForYAML($key) . ': ' . &escapeForYAML($$hashRef{$key}) . "\n";
  | 
        
        
            | 
            | 
           281 | 
                 }
  | 
        
        
            | 
            | 
           282 | 
              }
  | 
        
        
            | 
            | 
           283 | 
              return $output;
  | 
        
        
            | 
            | 
           284 | 
           }
  | 
        
        
            | 
            | 
           285 | 
              | 
        
        
            | 
            | 
           286 | 
              | 
        
        
            | 
            | 
           287 | 
           #######################################################
  | 
        
        
            | 
            | 
           288 | 
           #
  | 
        
        
            | 
            | 
           289 | 
           # tabDelimitedToHash ($hashRef, $tabdelim)
  | 
        
        
            | 
            | 
           290 | 
           #
  | 
        
        
            | 
            | 
           291 | 
           # Takes a tab delimited multi line string and adds it
  | 
        
        
            | 
            | 
           292 | 
           # to a hash. The final field in each line is considered to
  | 
        
        
            | 
            | 
           293 | 
           # be the value, and all prior fields are considered to be
  | 
        
        
            | 
            | 
           294 | 
           # hierachial keys.
  | 
        
        
            | 
            | 
           295 | 
           #
  | 
        
        
            | 
            | 
           296 | 
           # Parameters
  | 
        
        
            | 
            | 
           297 | 
           #     $hashref - reference (address) of a hash
  | 
        
        
            | 
            | 
           298 | 
           #     $tabdelim - A tab delimited, newline terminated set of records
  | 
        
        
            | 
            | 
           299 | 
           #
  | 
        
        
            | 
            | 
           300 | 
           #
  | 
        
        
            | 
            | 
           301 | 
           #######################################################
  | 
        
        
            | 
            | 
           302 | 
           sub tabDelimitedToHash {
  | 
        
        
            | 
            | 
           303 | 
              my ($hashRef, $tabdelim) = @_;
  | 
        
        
            | 
            | 
           304 | 
              foreach my $line ( split( "\n", $tabdelim ) ) { # split on newlines, then process each line in turn
  | 
        
        
            | 
            | 
           305 | 
                 $line =~ s/'/\\'/gi; # escape single quotes
  | 
        
        
            | 
            | 
           306 | 
                 @fields = split( / *\t */, $line ); # get all the field values into array
  | 
        
        
            | 
            | 
           307 | 
                 my $theValue = pop @fields; # the last one is the value, so save it
  | 
        
        
            | 
            | 
           308 | 
                 # now, we build a Perl statement that would create the assignment. The goal is
  | 
        
        
            | 
            | 
           309 | 
                 # to have a string that says something like $$hashRef{'key'}{'key'} = $value;
  | 
        
        
            | 
            | 
           310 | 
                 # then, eval that.
  | 
        
        
            | 
            | 
           311 | 
                 my $command = '$$hashRef'; # start with the name of the dereferenced hash (parameter 1)
  | 
        
        
            | 
            | 
           312 | 
                 while (my $key = shift @fields) { # while we have a key, from left to right
  | 
        
        
            | 
            | 
           313 | 
                    $command .= '{' . "'$key'" . '}'; # build it as {'key'} concated to string
  | 
        
        
            | 
            | 
           314 | 
                 }
  | 
        
        
            | 
            | 
           315 | 
                 $command .= "='$theValue';"; # add the assignment
  | 
        
        
            | 
            | 
           316 | 
                 #print STDERR "$command\n"; 
  | 
        
        
            | 
            | 
           317 | 
                 eval $command; # eval the string to make the actual assignment
  | 
        
        
            | 
            | 
           318 | 
              }
  | 
        
        
            | 
            | 
           319 | 
           }
  | 
        
        
            | 
            | 
           320 | 
              | 
        
        
            | 
            | 
           321 | 
           #######################################################
  | 
        
        
            | 
            | 
           322 | 
           #
  | 
        
        
            | 
            | 
           323 | 
           # ProcessModules ( $system, $moduleDir )
  | 
        
        
            | 
            | 
           324 | 
           #
  | 
        
        
            | 
            | 
           325 | 
           # Processes all modules in $moduleDir, adding result to $system hash
  | 
        
        
            | 
            | 
           326 | 
           # 
  | 
        
        
            | 
            | 
           327 | 
           # Parameters
  | 
        
        
            | 
            | 
           328 | 
           #     $system - reference (address) of a hash
  | 
        
        
            | 
            | 
           329 | 
           #     $moduleDir - full path to a directory containing executable scripts
  | 
        
        
            | 
            | 
           330 | 
           #  
  | 
        
        
            | 
            | 
           331 | 
           # Each file in the $moduleDir directory that matches the regex in the grep
  | 
        
        
            | 
            | 
           332 | 
           # and is executable is run. It is assumed the script will return 0 on success
  | 
        
        
            | 
            | 
           333 | 
           # or a non-zero on failure
  | 
        
        
            | 
            | 
           334 | 
           # The output of the script is assumed to be a tab delimited, newline separated
  | 
        
        
            | 
            | 
           335 | 
           # list of records that should be added to the hash $system. This is done by calling 
  | 
        
        
            | 
            | 
           336 | 
           # &parseModule above.
  | 
        
        
            | 
            | 
           337 | 
           # on failure, the returned output of the script is assumed to be an error message
  | 
        
        
            | 
            | 
           338 | 
           # and is displayed on STDERR
  | 
        
        
            | 
            | 
           339 | 
           #######################################################
  | 
        
        
            | 
            | 
           340 | 
           sub ProcessModules {
  | 
        
        
            | 
            | 
           341 | 
              my ( $system, $moduleDir ) = @_;
  | 
        
        
            | 
            | 
           342 | 
              # open the module directory
  | 
        
        
            | 
            | 
           343 | 
              opendir( my $dh, $moduleDir ) || die "Module Directory $moduleDir can not be opened: $!\n";
  | 
        
        
            | 
            | 
           344 | 
              # and get all files which are executable and contain nothing but alpha-numerics and underscores (must begin with alpha-numeric)
  | 
        
        
            | 
            | 
           345 | 
              my @modules = grep { /^[a-zA-Z0-9][a-zA-Z0-9_]+$/ && -x "$moduleDir/$_" } readdir( $dh );
  | 
        
        
            | 
            | 
           346 | 
              closedir $dh;
  | 
        
        
            | 
            | 
           347 | 
              foreach $modFile ( sort @modules ) { # for each valid script
  | 
        
        
            | 
            | 
           348 | 
                 print "Processing module $moduleDir$modFile\n" if $TESTING > 2;
  | 
        
        
            | 
            | 
           349 | 
                 my $output = qx/$moduleDir$modFile $moduleDir/; # execute it and grab the output
  | 
        
        
            | 
            | 
           350 | 
                 my $exitCode = $? >> 8; # process the exitCode
  | 
        
        
            | 
            | 
           351 | 
                 if ( $exitCode ) { # if non-zero, error, so show an error message
  | 
        
        
            | 
            | 
           352 | 
                    warn "Error in $moduleDir$modFile, [$output]\n";
  | 
        
        
            | 
            | 
           353 | 
                 } else { # otherwise, call tabDelimitedToHash to save the data
  | 
        
        
            | 
            | 
           354 | 
                    &tabDelimitedToHash( $system, $output );
  | 
        
        
            | 
            | 
           355 | 
                 }
  | 
        
        
            | 
            | 
           356 | 
              }
  | 
        
        
            | 
            | 
           357 | 
           }
  | 
        
        
            | 
            | 
           358 | 
              | 
        
        
            | 
            | 
           359 | 
           # load the configuration file
  | 
        
        
            | 
            | 
           360 | 
           eval ( &loadConfigurationFile($configurationFile) );
  | 
        
        
            | 
            | 
           361 | 
           # user did not define a serial number, so make something up
  | 
        
        
            | 
            | 
           362 | 
           $serialNumber = &getSerialNumber unless $serialNumber;
  | 
        
        
            | 
            | 
           363 | 
           # oops, no client name (required) so tell them and exit
  | 
        
        
            | 
            | 
           364 | 
           die "You must configure this package in $configurationFile" unless $clientName;
  | 
        
        
            | 
            | 
           365 | 
              | 
        
        
            | 
            | 
           366 | 
           my $System; # hash reference that will store all info we are going to send to the server
  | 
        
        
            | 
            | 
           367 | 
           # some defaults.
  | 
        
        
            | 
            | 
           368 | 
           $$System{'report'}{'version'} = $VERSION;
  | 
        
        
            | 
            | 
           369 | 
           $$System{'report'}{'date'} = $reportDate;
  | 
        
        
            | 
            | 
           370 | 
           $$System{'report'}{'client'} = $clientName;
  | 
        
        
            | 
            | 
           371 | 
           $$System{'system'}{'hostname'} = $hostname;
  | 
        
        
            | 
            | 
           372 | 
           $$System{'system'}{'serial'} = $serialNumber;
  | 
        
        
            | 
            | 
           373 | 
              | 
        
        
            | 
            | 
           374 | 
           # process any modules in the system
  | 
        
        
            | 
            | 
           375 | 
           &ProcessModules( $System, $moduleDir );
  | 
        
        
            | 
            | 
           376 | 
              | 
        
        
            | 
            | 
           377 | 
           # now, everything ins in $System, so convert it to the proper output format
  | 
        
        
            | 
            | 
           378 | 
           my $out =  "#sysinfo: $VERSION YAML\n---\n" . &hashToYAML( $System ) . "....\n";
  | 
        
        
            | 
            | 
           379 | 
              | 
        
        
            | 
            | 
           380 | 
           print Data::Dumper->Dump([$System],['System']) if $TESTING>3;
  | 
        
        
            | 
            | 
           381 | 
              | 
        
        
            | 
            | 
           382 | 
           # and send the results to the server
  | 
        
        
            | 
            | 
           383 | 
           &sendResults( $iSendReports, $out, $scriptsDir );
  | 
        
        
            | 
            | 
           384 | 
              | 
        
        
            | 
            | 
           385 | 
           1;
  |