mirror of
				https://github.com/pandorafms/pandorafms.git
				synced 2025-10-31 03:24:48 +01:00 
			
		
		
		
	* android bsd-ipso bsd-ipso/AUTHORS bsd-ipso/pandora_agent_daemon bsd-ipso/pandora_agent.conf bsd-ipso/pandora_user.conf bsd-ipso/COPYING bsd-ipso/pandora_agent.sh bsd-ipso/README aix aix/pandora_agent_installer aix/pandora_agent aix/AUTHORS aix/pandora_agent_daemon aix/pandora_agent.conf aix/pandora_user.conf aix/COPYING aix/README mac_osx mac_osx/pandora_agent_installer mac_osx/plugins mac_osx/plugins/ipmi2xml mac_osx/plugins/ipmi2xml/ipmi2xml.php mac_osx/plugins/ipmi2xml/README.txt mac_osx/plugins/XServeRAID mac_osx/plugins/XServeRAID/xserve-raid-tools-1.2-1.uwhep.sl4.i386.rpm mac_osx/plugins/XServeRAID/xserve-raid-tools-1.2-1.rhel3.i386.rpm mac_osx/plugins/XServeRAID/pandora_agent.conf mac_osx/plugins/XServeRAID/check_xserve mac_osx/plugins/XServeRAID/xserve-raid-tools-1.2.tar.gz mac_osx/plugins/XServeRAID/README.txt mac_osx/plugins/SGE mac_osx/plugins/SGE/SGEStatus.sh mac_osx/plugins/SGE/README mac_osx/plugins/grep_log mac_osx/plugins/ppc_sensors mac_osx/pandora_agent mac_osx/AUTHORS mac_osx/pandora_agent.conf mac_osx/ChangeLog mac_osx/COPYING mac_osx/pandora_fms mac_osx/pandora_fms/StartupParameters.plist mac_osx/pandora_fms/pandora_fms mac_osx/Pandora FMS.pmdoc mac_osx/Pandora FMS.pmdoc/01pandora-contents.xml mac_osx/Pandora FMS.pmdoc/01pandora.xml mac_osx/Pandora FMS.pmdoc/index.xml mac_osx/README mac_osx/tentacle_client shellscript shellscript/linux shellscript/bsd-ipso shellscript/hp-ux shellscript/aix shellscript/meta_agent shellscript/mac_osx shellscript/solaris shellscript/openWRT openWRT openWRT/pandora_agent_async openWRT/pandora_serialread openWRT/contrib openWRT/contrib/pandora_agent_installer openWRT/contrib/S55pandora openWRT/contrib/pandora_agent_daemon openWRT/pandora_getsync openWRT/pandora_agent openWRT/AUTHORS openWRT/pandora_agent.conf openWRT/pandora_arduino.c openWRT/pandora_user.conf openWRT/COPYING openWRT/README.openwrt openWRT/README linux linux/pandora_agent_installer linux/plugins linux/plugins/inventory linux/plugins/pandora_df linux/plugins/grep_log linux/DEBIAN linux/DEBIAN/control linux/DEBIAN/postinst linux/DEBIAN/prerm linux/DEBIAN/conffiles linux/DEBIAN/make_deb_package.sh linux/DEBIAN/md5sums linux/pandora_agent linux/AUTHORS linux/pandora_agent_daemon linux/pandora_agent.conf linux/COPYING linux/pandora_agent.spec linux/README linux/tentacle_client hp-ux hp-ux/pandora_agent_installer hp-ux/pandora_agent hp-ux/AUTHORS hp-ux/pandora_agent_daemon hp-ux/pandora_agent.conf hp-ux/COPYING hp-ux/README meta_agent meta_agent/data_out meta_agent/pandora_agent_sim.sh meta_agent/AUTHORS meta_agent/pandora_agent_daemon meta_agent/INSTALL meta_agent/pandora_agent_1.conf meta_agent/pandora_agent_2.conf meta_agent/COPYING meta_agent/pandora_agent_3.conf meta_agent/pandora_agent_4.conf meta_agent/pandora_agent_5.conf meta_agent/README meta_agent/muestra_config.sh solaris solaris/pandora_agent_installer solaris/data_out solaris/pandora_agent solaris/AUTHORS solaris/pandora_agent_daemon solaris/pandora_agent.conf solaris/pandora_user.conf solaris/COPYING solaris/README: Reorganise the shellscript moving all the sh scripts to one folder git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3778 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
		
			
				
	
	
		
			283 lines
		
	
	
		
			7.3 KiB
		
	
	
	
		
			Perl
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			283 lines
		
	
	
		
			7.3 KiB
		
	
	
	
		
			Perl
		
	
	
		
			Executable File
		
	
	
	
	
| #!/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';
 | |
| 
 | |
| # Parse module information
 | |
| sub get_module_data ($$$$) {
 | |
| 	my ($name, $hwinfo, $keys, $modules) = @_;
 | |
| 	my %module;
 | |
| 
 | |
| 	# Parse module data
 | |
| 	while (my $line = shift (@{$hwinfo})) {
 | |
| 		if ($line =~ /\s+\*\-/) {
 | |
| 			unshift (@{$hwinfo}, $line);
 | |
| 			last;
 | |
| 		}
 | |
| 		foreach my $key (@{$keys}) {
 | |
| 			if ($line =~ /$key:\s+(.+)/) {
 | |
| 				$module{$key} = $1;
 | |
| 				push (@{$module{'_keys'}}, $key);
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	# 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) = @_;
 | |
| 	my $runlevel = `runlevel | cut -d' ' -f2`;
 | |
| 
 | |
| 	#ini trim($runlevel)
 | |
| 		$runlevel =~ s/^\s*//; #ltrim
 | |
| 		$runlevel =~ s/\s*$//; #rtrim
 | |
| 	#end trim($runlevel)
 | |
| 
 | |
| 	my $script = "ls /etc/rc" . $runlevel .".d/ -l | grep \"^l.*\" | grep \" S.* \" | cut -d' ' -f11";
 | |
| 
 | |
| 	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) = @_;
 | |
| 
 | |
| 	my $script = "ps aux";
 | |
| 
 | |
|         my @services = `$script`;
 | |
|         foreach my $row (@services) {
 | |
|                 my %module;
 | |
| 				# Remove carriage returns				
 | |
| 				$row =~ s/[\n\l\f]//g;
 | |
|                 $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
 | |
| 	open (RELEASE_FILE, '< /etc/lsb-release') or return;
 | |
| 	my $distrib_id = <RELEASE_FILE>;
 | |
| 	last unless ($distrib_id =~ m/DISTRIB_ID\s*=\s*(\S+)/);
 | |
| 	$distrib_id = $1;
 | |
| 
 | |
| 	# List installed programs
 | |
| 	my @soft;
 | |
| 	if ($distrib_id eq 'Ubuntu') {
 | |
| 		@soft = `dpkg -l | grep ii`;
 | |
| 	} else {
 | |
| 		return;
 | |
| 	}
 | |
| 
 | |
| 	# 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;
 | |
| 		$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
 | |
| 		my $data = '';
 | |
| 		foreach my $key (@{$item->{'_keys'}}) {
 | |
| 			next unless defined ($item->{$key});
 | |
| 			$data .= ($data eq '' ? '' : ';') . $item->{$key};
 | |
| 		}
 | |
| 
 | |
| 		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;
 | |
| my $enable_all = ($#ARGV > 0 ? 0 : 1);
 | |
| my $interval = shift (@ARGV);
 | |
| foreach my $module (@ARGV) {
 | |
| 	$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
 | |
| my @hwinfo = `lshw 2>/dev/null`;
 | |
| 
 | |
| # Parse hardware information
 | |
| my %modules;
 | |
| while (my $line = shift (@hwinfo)) {
 | |
| 	chomp ($line);
 | |
| 
 | |
| 	# CPU
 | |
| 	if ($line =~ /\*\-cpu/ && ($enable_all == 1 || $enabled{'cpu'} == 1)) {
 | |
| 		get_module_data ('CPU', \@hwinfo, ['product', 'vendor', 'capacity'], \%modules);
 | |
| 	}
 | |
| 
 | |
| 	# RAM
 | |
| 	if ($line =~ /\*\-bank/ && ($enable_all == 1 || $enabled{'ram'} == 1)) {
 | |
| 		get_module_data ('RAM', \@hwinfo, ['description', 'size'], \%modules);
 | |
| 	}
 | |
| 
 | |
| 	# VIDEO
 | |
| 	if ($line =~ /\*\-display/ && ($enable_all == 1 || $enabled{'video'} == 1)) {
 | |
| 		get_module_data ('VIDEO', \@hwinfo, ['product', 'description', 'vendor'], \%modules);
 | |
| 	}
 | |
| 
 | |
| 	# NIC
 | |
| 	if ($line =~ /\*\-network/ && ($enable_all == 1 || $enabled{'nic'} == 1)) {
 | |
| 		get_module_data ('NIC', \@hwinfo, ['product', 'description', 'vendor'], \%modules);
 | |
| 	}
 | |
| 
 | |
| 	# CDROM
 | |
| 	if ($line =~ /\*\-cdrom/ && ($enable_all == 1 || $enabled{'cdrom'} == 1)) {
 | |
| 		get_module_data ('CDROM', \@hwinfo, ['product', 'description', 'vendor'], \%modules);
 | |
| 	}
 | |
| 
 | |
| 	# HD
 | |
| 	if ($line =~ /\*\-disk/ && ($enable_all == 1 || $enabled{'hd'} == 1)) {
 | |
| 		get_module_data ('HD', \@hwinfo, ['product', 'description', 'size'], \%modules);
 | |
| 	}
 | |
| }
 | |
| 
 | |
| # 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;
 |