| 1 | 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 a key/value pair, with the key in square brackets, example:
 | 
        
           |  |  | 9 | #  [tag]value
 | 
        
           |  |  | 10 | # where tag is the name of the function we are looking at (hostname, report date, cpu type)
 | 
        
           |  |  | 11 | # and value is the associated value.
 | 
        
           |  |  | 12 | # Also has multi line key/values, which are in the form
 | 
        
           |  |  | 13 | #   [tag]
 | 
        
           |  |  | 14 | #   value1
 | 
        
           |  |  | 15 | #   value2
 | 
        
           |  |  | 16 | #   [another tag (or end of file)]
 | 
        
           |  |  | 17 | # where value1 and value2 are considered attributes of tag. This is used where there are multiple
 | 
        
           |  |  | 18 | # values for a function, such as for installed packages, IP Addresses, hardware lists, and 
 | 
        
           |  |  | 19 | # disk partitions.
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | # Assumes following packages installed on system:
 | 
        
           |  |  | 22 | # hostname
 | 
        
           |  |  | 23 | # uname
 | 
        
           |  |  | 24 | # procinfo
 | 
        
           |  |  | 25 | # uptime
 | 
        
           |  |  | 26 | # lspci
 | 
        
           |  |  | 27 | # lsb_release
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | # apt-get install procinfo lsb sysinfo
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | # Will search for one of the following package managers:
 | 
        
           |  |  | 32 | # dpkg (Debian and derivatives)
 | 
        
           |  |  | 33 | # rpm  (RedHat and derivatives)
 | 
        
           |  |  | 34 | # yast (SuSE and derivatives) NOT IMPLEMENTED
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 | # Also assumes following standard apps are accessible
 | 
        
           |  |  | 37 | # ifconfig
 | 
        
           |  |  | 38 | # grep
 | 
        
           |  |  | 39 | # awk
 | 
        
           |  |  | 40 | # cat
 | 
        
           |  |  | 41 | # df
 | 
        
           |  |  | 42 | # free
 | 
        
           |  |  | 43 | #
 | 
        
           |  |  | 44 | # Version 1.3 20071104
 | 
        
           |  |  | 45 | # added capability of e-mailing the results by itself and external configuration file
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | # Version 1.3.1 20071110
 | 
        
           |  |  | 48 | # added du -sk to explicitly do directory sizes in 'k'. Also, fixed some documentation
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 | # Version 1.3.3 20081104
 | 
        
           |  |  | 51 | # modified hostname to hostname -f, and allowed user to place custom value in configuration file
 | 
        
           |  |  | 52 | # also, modified to go with Debian standards in preparation to creating a debian package.
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 | # Version 2.0 20081208
 | 
        
           |  |  | 55 | # Modified to use different libraries for different OS's in preparation to porting to Windows
 | 
        
           |  |  | 56 | # Uses different packages based on which OS it is on.
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 | # Following are global variables overridden if configuration file exists
 | 
        
           |  |  | 59 | my $APPLICATION_ROOT;
 | 
        
           |  |  | 60 | my $client_name;
 | 
        
           |  |  | 61 | my $CUSTOM_PACKAGE_FINDER ;
 | 
        
           |  |  | 62 | my @directoriesToWatch;
 | 
        
           |  |  | 63 | my $hostname;
 | 
        
           |  |  | 64 | # only required if reports are sent from process_sysinfo.pl. Not the norm.
 | 
        
           |  |  | 65 | my $iMailResults = 0; # if 0 (false), ignore following variables
 | 
        
           |  |  | 66 | my $mailTo;
 | 
        
           |  |  | 67 | my $mailCC;
 | 
        
           |  |  | 68 | my $mailBCC;
 | 
        
           |  |  | 69 | my $mailServer;
 | 
        
           |  |  | 70 | my $mailServerPort;
 | 
        
           |  |  | 71 | my $mailFrom;
 | 
        
           |  |  | 72 | my $mailSubject = `hostname` . ' sysinfo ' . `date +"%F"`;
 | 
        
           |  |  | 73 | my $SENDMAIL;
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 | my $TESTING = 0;
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 | my $VERSION = '2.0.0';
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 | sub loadConfigurationFile {
 | 
        
           |  |  | 80 |    my $configuration_file;
 | 
        
           |  |  | 81 |    if ($TESTING) {
 | 
        
           |  |  | 82 |       $configuration_file = './sysinfo.conf';
 | 
        
           |  |  | 83 |    } else {
 | 
        
           |  |  | 84 |       $configuration_file = '/etc/sysinfo/sysinfo.conf';
 | 
        
           |  |  | 85 |    }
 | 
        
           |  |  | 86 |    open CONFFILE, "<$configuration_file" or die "Can not open configuration file $configuration_file";
 | 
        
           |  |  | 87 |    my $confFileContents = join( '', <CONFFILE> );
 | 
        
           |  |  | 88 |    close CONFFILE;
 | 
        
           |  |  | 89 |    return $confFileContents;
 | 
        
           |  |  | 90 | }
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 | # generic routine to send an e-mail
 | 
        
           |  |  | 93 | sub sendmessage {
 | 
        
           |  |  | 94 |    my ( $from, $to, $subject, $message, $cc, $bcc, $server, $port ) = @_;
 | 
        
           |  |  | 95 |    open SENDMAIL, "|$SENDMAIL" or die "Could not open sendmail";
 | 
        
           |  |  | 96 |    print SENDMAIL "From: $from\nTo: $to\nSubject: $subject\n";
 | 
        
           |  |  | 97 |    print SENDMAIL "cc: $cc\n" if $cc;
 | 
        
           |  |  | 98 |    print SENDMAIL "bcc: $bcc\n" if $bcc;
 | 
        
           |  |  | 99 |    print SENDMAIL "\n$message\n";
 | 
        
           |  |  | 100 |    print SENDMAIL ".\n";
 | 
        
           |  |  | 101 |    close SENDMAIL;
 | 
        
           |  |  | 102 | }
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 | BEGIN { # block to find home grown libraries.
 | 
        
           |  |  | 108 |    push @INC, '.'; # here is where local library is
 | 
        
           |  |  | 109 | }
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 | if ($TESTING) {
 | 
        
           |  |  | 112 |    use Data::Dumper;
 | 
        
           |  |  | 113 | }
 | 
        
           |  |  | 114 | # Windows:  ppm install XML::Simple
 | 
        
           |  |  | 115 | # Debian: apt-get install libxml-simple-perl
 | 
        
           |  |  | 116 | use XML::Simple; 
 | 
        
           |  |  | 117 | use SysinfoLinux;
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 | eval( &loadConfigurationFile ); # load the configuration file
 | 
        
           |  |  | 120 | # parameters are assumed to override configuration file details. If we have parameters
 | 
        
           |  |  | 121 | # they are of the form varname=value, where value is the new value to assign to varname
 | 
        
           |  |  | 122 | # the command has a dollar sign prepended (for the variable) and a semi-colon appended
 | 
        
           |  |  | 123 | # to create a command that may be executed by eval. Thus, this must be a single line
 | 
        
           |  |  | 124 | # no embedded spaces parameter.
 | 
        
           |  |  | 125 | #while ($parameter = shift ) {
 | 
        
           |  |  | 126 | #   eval ( '$' . $parameter . ';');
 | 
        
           |  |  | 127 | #}
 | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 | die "You must configure this package in /etc/sysinfo/sysinfo.conf" unless $client_name;
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 | my $commandLine;
 | 
        
           |  |  | 132 | if (@ARGV) {
 | 
        
           |  |  | 133 |    $commandLine = join("\n", @ARGV) . "\n\n";
 | 
        
           |  |  | 134 | }
 | 
        
           |  |  | 135 |   | 
        
           |  |  | 136 | my $System;
 | 
        
           |  |  | 137 | $$System{'report'}{'version'} = $VERSION;
 | 
        
           |  |  | 138 | $$System{'report'}{'date'} = `date '+%Y-%m-%d %H:%M'`;
 | 
        
           |  |  | 139 | $$System{'report'}{'client'} = $client_name;
 | 
        
           |  |  | 140 | chomp $$System{'report'}{'date'};
 | 
        
           |  |  | 141 | $$System{'network'} = &SysinfoLinux::getNetwork;
 | 
        
           |  |  | 142 | $$System{'diskinfo'} = &SysinfoLinux::getDiskInfo(@directoriesToWatch);
 | 
        
           |  |  | 143 | $$System{'software'} = &SysinfoLinux::getSoftware($CUSTOM_PACKAGE_FINDER);
 | 
        
           |  |  | 144 | $$System{'operatingsystem'} = &SysinfoLinux::getOperatingSystem;
 | 
        
           |  |  | 145 | $$System{'system'} = &SysinfoLinux::getSystemInformation($hostname);
 | 
        
           |  |  | 146 | $$System{'pci'} = &SysinfoLinux::getPCI;
 | 
        
           |  |  | 147 |   | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 | my $output = XML::Simple->new(AttrIndent => 1, KeepRoot => 1, NoAttr => 1, RootName => "sysinfo$VERSION" ); # NoIndent => 1
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |   | 
        
           |  |  | 152 | #print Data::Dumper->Dump([$System],['System']);
 | 
        
           |  |  | 153 |   | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 | if ($iMailResults) {
 | 
        
           |  |  | 156 |    &sendmessage( $mailFrom, $mailTo, $mailSubject, $output->XMLout($System), $mailCC, $mailBCC, $mailServer, $mailServerPort );
 | 
        
           |  |  | 157 | } else {
 | 
        
           |  |  | 158 |    print $output->XMLout($System);
 | 
        
           |  |  | 159 | }
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 | 1;
 |