2009-11-05 19:26:54 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Copyright (c) 2009 Artica Soluciones Tecnologicas S.L.
|
|
|
|
#
|
|
|
|
# inventory Generate a hardware/software inventory.
|
|
|
|
#
|
|
|
|
# Sample usage: ./inventory <interval in days> [cpu] [ram] [video] [nic] [hd] [cdrom] [software] [init_services] [filesystem] [process] [users]
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; version 2 of the License.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use constant TSTAMP_FILE => '/tmp/pandora_inventory.tstamp';
|
|
|
|
|
2011-09-05 15:09:21 +02:00
|
|
|
# Operation mode (LSHW or HWINFO)
|
|
|
|
my $Mode;
|
|
|
|
|
|
|
|
# Item separator
|
|
|
|
my $Separator;
|
|
|
|
|
2009-11-05 19:26:54 +01:00
|
|
|
# Parse module information
|
|
|
|
sub get_module_data ($$$$) {
|
|
|
|
my ($name, $hwinfo, $keys, $modules) = @_;
|
|
|
|
my %module;
|
2012-04-25 17:07:33 +02:00
|
|
|
|
|
|
|
# Store keys
|
|
|
|
foreach my $key (@{$keys}) {
|
|
|
|
push (@{$module{'_keys'}}, $key);
|
|
|
|
}
|
|
|
|
|
2009-11-05 19:26:54 +01:00
|
|
|
# Parse module data
|
|
|
|
while (my $line = shift (@{$hwinfo})) {
|
2011-09-05 15:09:21 +02:00
|
|
|
if ($line =~ /$Separator/) {
|
2009-11-05 19:26:54 +01:00
|
|
|
unshift (@{$hwinfo}, $line);
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
foreach my $key (@{$keys}) {
|
|
|
|
if ($line =~ /$key:\s+(.+)/) {
|
|
|
|
$module{$key} = $1;
|
2012-04-25 17:07:33 +02:00
|
|
|
# Replace semicolon by comma to avoid parse errors
|
|
|
|
$module{$key} =~ s/;/,/g;
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# No data found
|
|
|
|
my @data = keys (%module);
|
|
|
|
return unless ($#data >= 0);
|
|
|
|
|
|
|
|
push (@{$modules->{$name}}, \%module);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get a list of information file system in machine
|
|
|
|
sub get_file_system($$) {
|
|
|
|
my ($name, $modules) = @_;
|
|
|
|
|
|
|
|
my @fileSystems = `df -h | tail -n +2`; #remove the titles of columns
|
|
|
|
|
|
|
|
foreach my $row (@fileSystems) {
|
|
|
|
next unless ($row =~ /^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+\S+\s+(\S+)/);
|
|
|
|
|
|
|
|
my %module;
|
|
|
|
$module{'filesystem'} = $1;
|
|
|
|
$module{'used'} = $2;
|
|
|
|
$module{'avail'} = $3;
|
|
|
|
$module{'mount'} = $4;
|
|
|
|
$module{'_keys'} = ['filesystem', 'used','avail', 'mount'];
|
|
|
|
push (@{$modules->{$name}}, \%module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get a list of services init in machine
|
|
|
|
sub get_servicies_init_machine($$) {
|
|
|
|
my ($name, $modules) = @_;
|
2012-04-25 18:47:03 +02:00
|
|
|
my $runlevel = `who -r | awk '{print \$2}'`;
|
2009-11-05 19:26:54 +01:00
|
|
|
|
|
|
|
#ini trim($runlevel)
|
|
|
|
$runlevel =~ s/^\s*//; #ltrim
|
|
|
|
$runlevel =~ s/\s*$//; #rtrim
|
|
|
|
#end trim($runlevel)
|
|
|
|
|
2012-04-25 17:55:26 +02:00
|
|
|
my $script = "";
|
|
|
|
|
|
|
|
if (-e "/etc/rc" . $runlevel .".d/") {
|
|
|
|
$script = "ls /etc/rc" . $runlevel .".d/ -l | grep \"^l.*\" | grep \" S.* \" | cut -d' ' -f11";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$script = "ls /etc/rc.d/rc" . $runlevel .".d/ -l | grep \"^l.*\" | grep \" S.* \" | cut -d' ' -f11";
|
|
|
|
}
|
|
|
|
|
2009-11-05 19:26:54 +01:00
|
|
|
my @services = `$script`;
|
|
|
|
foreach my $row (@services) {
|
|
|
|
next unless ($row =~ /^\S+\/(\S+)$/);
|
|
|
|
|
|
|
|
my %module;
|
|
|
|
$module{'service'} = $1;
|
|
|
|
$module{'_keys'} = ['service'];
|
|
|
|
push (@{$modules->{$name}}, \%module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get a list of running processes
|
|
|
|
sub get_processes ($$) {
|
|
|
|
my ($name, $modules) = @_;
|
|
|
|
|
2010-10-04 19:22:28 +02:00
|
|
|
my $script = "ps -eo command";
|
2009-11-05 19:26:54 +01:00
|
|
|
|
|
|
|
my @services = `$script`;
|
|
|
|
foreach my $row (@services) {
|
|
|
|
my %module;
|
|
|
|
# Remove carriage returns
|
|
|
|
$row =~ s/[\n\l\f]//g;
|
2012-04-25 17:07:33 +02:00
|
|
|
# Replace semicolon by comma to avoid parse errors
|
|
|
|
$row =~ s/;/,/g;
|
2009-11-05 19:26:54 +01:00
|
|
|
$module{'service'} = $row;
|
|
|
|
$module{'_keys'} = ['service'];
|
|
|
|
push (@{$modules->{$name}}, \%module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get a list of valid users in the system
|
|
|
|
sub get_users ($$) {
|
|
|
|
my ($name, $modules) = @_;
|
|
|
|
|
|
|
|
my $script = "cat /etc/passwd";
|
|
|
|
my $user = "";
|
|
|
|
my $estado = "";
|
|
|
|
|
|
|
|
my @services = `$script`;
|
|
|
|
foreach my $row (@services) {
|
|
|
|
my %module;
|
|
|
|
|
|
|
|
next unless ($row =~ /^([A-Za-z0-9\-\_]*)/);
|
|
|
|
|
|
|
|
$user = $1;
|
|
|
|
$script = `passwd -S $user`;
|
|
|
|
if ( $script =~ /^(\S+)\sP./){
|
|
|
|
$module{'user'} = $user;
|
|
|
|
$module{'_keys'} = ['user'];
|
|
|
|
push (@{$modules->{$name}}, \%module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get a list of installed programs
|
|
|
|
sub get_software_module_data ($$) {
|
|
|
|
my ($name, $modules) = @_;
|
|
|
|
|
|
|
|
# Guess the current distribution
|
2009-12-07 14:20:24 +01:00
|
|
|
my $distrib_id = "";
|
|
|
|
|
|
|
|
if ( -e "/etc/SuSE-release"){
|
|
|
|
$distrib_id = "SUSE";
|
|
|
|
} else {
|
|
|
|
open (RELEASE_FILE, '< /etc/lsb-release') or return;
|
|
|
|
$distrib_id = <RELEASE_FILE>;
|
|
|
|
last unless ($distrib_id =~ m/DISTRIB_ID\s*=\s*(\S+)/);
|
|
|
|
$distrib_id = $1;
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
|
|
|
|
# List installed programs
|
|
|
|
my @soft;
|
|
|
|
if ($distrib_id eq 'Ubuntu') {
|
|
|
|
@soft = `dpkg -l | grep ii`;
|
|
|
|
} else {
|
2009-12-07 14:20:24 +01:00
|
|
|
|
|
|
|
@soft = `rpm -q -a --qf "ii\t%{NAME}\t%{VERSION}\t%{SUMMARY}\n"`;
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
2009-12-07 14:20:24 +01:00
|
|
|
|
2009-11-05 19:26:54 +01:00
|
|
|
# Parse data
|
|
|
|
foreach my $row (@soft) {
|
|
|
|
next unless ($row =~ /^ii\s+(\S+)\s+(\S+)\s+([^\n]+)/);
|
|
|
|
|
|
|
|
my %module;
|
|
|
|
$module{'program'} = $1;
|
|
|
|
$module{'version'} = $2;
|
|
|
|
$module{'description'} = $3;
|
2012-04-25 17:07:33 +02:00
|
|
|
# Replace semicolon by comma to avoid parse errors
|
|
|
|
$module{'program'} =~ s/;/,/g;
|
|
|
|
$module{'version'} =~ s/;/,/g;
|
|
|
|
$module{'description'} =~ s/;/,/g;
|
2009-11-05 19:26:54 +01:00
|
|
|
$module{'_keys'} = ['program', 'version','description'];
|
|
|
|
push (@{$modules->{$name}}, \%module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Print module data
|
|
|
|
sub print_module ($$) {
|
|
|
|
my ($name, $module) = @_;
|
|
|
|
|
|
|
|
print " <inventory_module>\n";
|
|
|
|
print " <name><![CDATA[" . $name . "]]></name>\n";
|
|
|
|
print " <datalist>\n";
|
|
|
|
foreach my $item (@{$module}) {
|
|
|
|
# Compose module data
|
2012-04-25 17:07:33 +02:00
|
|
|
my $data = undef;
|
2009-11-05 19:26:54 +01:00
|
|
|
foreach my $key (@{$item->{'_keys'}}) {
|
2012-04-25 17:07:33 +02:00
|
|
|
$data = (!defined($data) ? '' : "$data;");
|
|
|
|
if (defined($item->{$key})) {
|
|
|
|
$data .= $item->{$key};
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
print " <data><![CDATA[$data]]></data>\n";
|
|
|
|
}
|
|
|
|
print " </datalist>\n";
|
|
|
|
print " </inventory_module>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check command line parameters
|
|
|
|
if ($#ARGV < 0) {
|
|
|
|
print "Usage: $0 <interval> [cpu] [ram] [video] [nic] [hd] [cdrom] [software] [init_services] [filesystem] [users] [process] \n\n";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Parse command line parameters
|
|
|
|
my %enabled;
|
2009-12-07 14:20:24 +01:00
|
|
|
my $interval = 1;
|
|
|
|
my $enable_all = 0;
|
|
|
|
|
|
|
|
$interval = $ARGV[0];
|
|
|
|
if ($#ARGV == 0){
|
|
|
|
$enable_all = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $module (@ARGV) {
|
|
|
|
if ($module eq "all"){
|
|
|
|
$enable_all = 1;
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
$enabled{$module} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check execution interval
|
|
|
|
if (-f TSTAMP_FILE) {
|
|
|
|
open (FILE, '<' . TSTAMP_FILE) || exit 1;
|
|
|
|
my $last_execution = <FILE>;
|
|
|
|
close (FILE);
|
|
|
|
exit 0 if ($last_execution + 86400 * $interval > time ());
|
|
|
|
}
|
|
|
|
open (FILE, '>' . TSTAMP_FILE) || exit 1;
|
|
|
|
print FILE time ();
|
|
|
|
close (FILE);
|
|
|
|
|
|
|
|
# Retrieve hardware information
|
2011-09-05 15:09:21 +02:00
|
|
|
$Mode = 'LSHW';
|
2012-04-25 17:07:33 +02:00
|
|
|
$Separator = '\s+\*\-';
|
2009-11-05 19:26:54 +01:00
|
|
|
my @hwinfo = `lshw 2>/dev/null`;
|
2011-09-05 15:09:21 +02:00
|
|
|
if ($? != 0) {
|
|
|
|
$Mode = 'HWINFO';
|
|
|
|
$Separator = 'Hardware Class:';
|
|
|
|
@hwinfo = `hwinfo --cpu --memory --gfxcard --netcard --cdrom --disk 2>/dev/null`;
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
|
|
|
|
# Parse hardware information
|
|
|
|
my %modules;
|
|
|
|
while (my $line = shift (@hwinfo)) {
|
|
|
|
chomp ($line);
|
|
|
|
|
|
|
|
# CPU
|
2011-09-05 15:09:21 +02:00
|
|
|
if (($line =~ /\*\-cpu/ || $line =~ /Hardware Class: cpu/) && ($enable_all == 1 || $enabled{'cpu'} == 1)) {
|
|
|
|
if ($Mode eq 'LSHW') {
|
|
|
|
get_module_data ('CPU', \@hwinfo, ['product', 'vendor', 'capacity'], \%modules);
|
|
|
|
} else {
|
|
|
|
get_module_data ('CPU', \@hwinfo, ['Model', 'Vendor', 'Clock'], \%modules);
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# RAM
|
2011-09-05 15:09:21 +02:00
|
|
|
if (($line =~ /\*\-bank/ || $line =~ /Hardware Class: memory/) && ($enable_all == 1 || $enabled{'ram'} == 1)) {
|
|
|
|
if ($Mode eq 'LSHW') {
|
|
|
|
get_module_data ('RAM', \@hwinfo, ['description', 'size'], \%modules);
|
|
|
|
} else {
|
|
|
|
get_module_data ('RAM', \@hwinfo, ['Model', 'Memory Size'], \%modules);
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# VIDEO
|
2011-09-05 15:09:21 +02:00
|
|
|
if (($line =~ /\*\-display/ || $line =~ /Hardware Class: graphics card/) && ($enable_all == 1 || $enabled{'video'} == 1)) {
|
|
|
|
if ($Mode eq 'LSHW') {
|
|
|
|
get_module_data ('VIDEO', \@hwinfo, ['product', 'description', 'vendor'], \%modules);
|
|
|
|
} else {
|
|
|
|
# Spaces before Device and Vendor are intentional to avoid matching SubDevice and SubVendor
|
|
|
|
get_module_data ('VIDEO', \@hwinfo, ['Model', ' Device', ' Vendor'], \%modules);
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# NIC
|
2011-09-05 15:09:21 +02:00
|
|
|
if (($line =~ /\*\-network/ || $line =~ /Hardware Class: network/) && ($enable_all == 1 || $enabled{'nic'} == 1)) {
|
|
|
|
if ($Mode eq 'LSHW') {
|
|
|
|
get_module_data ('NIC', \@hwinfo, ['product', 'description', 'vendor'], \%modules);
|
|
|
|
} else {
|
|
|
|
# Spaces before Device and Vendor are intentional to avoid matching SubDevice and SubVendor
|
|
|
|
get_module_data ('NIC', \@hwinfo, ['Model', ' Device', ' Vendor'], \%modules);
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# CDROM
|
2011-09-05 15:09:21 +02:00
|
|
|
if (($line =~ /\*\-cdrom/ || $line =~ /Hardware Class: cdrom/) && ($enable_all == 1 || $enabled{'cdrom'} == 1)) {
|
|
|
|
if ($Mode eq 'LSHW') {
|
|
|
|
get_module_data ('CDROM', \@hwinfo, ['product', 'description', 'vendor'], \%modules);
|
|
|
|
} else {
|
|
|
|
# Spaces before Device and Vendor are intentional to avoid matching SubDevice and SubVendor
|
|
|
|
get_module_data ('CDROM', \@hwinfo, ['Model', ' Device', ' Vendor'], \%modules);
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# HD
|
2011-09-05 15:09:21 +02:00
|
|
|
if (($line =~ /\*\-disk/ || $line =~ /Hardware Class: disk/) && ($enable_all == 1 || $enabled{'hd'} == 1)) {
|
|
|
|
if ($Mode eq 'LSHW') {
|
|
|
|
get_module_data ('HD', \@hwinfo, ['product', 'description', 'size'], \%modules);
|
|
|
|
} else {
|
|
|
|
get_module_data ('HD', \@hwinfo, ['Model', 'Serial ID', 'Size'], \%modules);
|
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Software
|
|
|
|
if ($enable_all == 1 || $enabled{'software'} == 1) {
|
|
|
|
get_software_module_data ('Software', \%modules);
|
|
|
|
}
|
|
|
|
|
|
|
|
#init_services
|
|
|
|
if ($enable_all == 1 || $enabled{'init_services'} == 1) {
|
|
|
|
get_servicies_init_machine ('Init services', \%modules);
|
|
|
|
}
|
|
|
|
|
|
|
|
#filesystem
|
|
|
|
if ($enable_all == 1 || $enabled{'filesystem'} == 1) {
|
|
|
|
get_file_system('File system', \%modules);
|
|
|
|
}
|
|
|
|
|
|
|
|
#processes
|
|
|
|
if ($enable_all == 1 || $enabled{'process'} == 1) {
|
|
|
|
get_processes('Process', \%modules);
|
|
|
|
}
|
|
|
|
|
|
|
|
#users
|
|
|
|
if ($enable_all == 1 || $enabled{'users'} == 1){
|
|
|
|
get_users ('Users', \%modules);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Print module data
|
|
|
|
print "<inventory>\n";
|
|
|
|
while (my ($name, $module) = each (%modules)) {
|
|
|
|
print_module ($name, $module);
|
|
|
|
}
|
|
|
|
print "</inventory>\n";
|
|
|
|
|
|
|
|
exit 0;
|