end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

サーバにinstallされているOSやMWの一覧を自動収集したい (centos版)

以下のように perl script書いて、実行したものを xymonあたりで収集すれば、OKな気がします

#!/usr/local/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $OS_INFO_CMD = 'cat /etc/system-release';
my $RPM_LIST_CMD = 'rpm -aq';  # = yum list installed
my @SRC_LIST_CMDS =
    (["mysql",              "/usr/local/mysql/bin/mysql --version"],
     ["perl",               "/usr/local/bin/perl -v"],
     ["java",               "/usr/bin/java -version"],
     ["apache httpd",       "/home/end0tknr/local/apache24/bin/httpd -v"]);

main();
exit(0);

sub main {

    my @sw_list;
    my $os_info  = installed_os_info();
    push(@sw_list,$os_info);
    my $src_list = installed_src_list();
    push(@sw_list,@$src_list);
    my $rpm_list = installed_rpm_list();
    push(@sw_list,@$rpm_list);

    for my $sw_info ( @sw_list ){
        print join("\t",@$sw_info),"\n";
    }
}


sub installed_os_info {

    my $fh;

    my $cmd = $OS_INFO_CMD;
    open($fh,"-|", $cmd) or die "fail open $cmd $!";
    my ($line) =<$fh>;
    close($fh) or die "fail close $cmd $!";

    chomp($line);
    return ["OS",$line];
}

sub installed_src_list {
    
    my @ret;
    for my $cmd ( @SRC_LIST_CMDS ) {
        my $fh;
        open($fh,"-|", "$cmd->[1] 2>&1") or die "fail open $cmd->[1] $!";
        
        my $installed_info = "";
        for my $line ( <$fh> ){
            chomp($line);
            if($line){
                $installed_info = $line;
                push(@ret,["MW",$cmd->[0],$installed_info]);
                last;
            }
        }
        unless( $installed_info ){
            die "fail find installed info $cmd->[1] $!";
        }
        close($fh) or die "fail close $cmd $!";
    }
    return \@ret;
}

sub installed_rpm_list {

    my $fh;
    open($fh,"-|", $RPM_LIST_CMD) or die "fail open $RPM_LIST_CMD $!";

    my @ret;
    for my $line ( sort <$fh> ){
        chomp($line);
        push(@ret,["MW",$line]);
    }
    
    close($fh) or die "fail close $RPM_LIST_CMD $!";

    return \@ret;
}

1;
$ ./get_inventory.pl 
OS  CentOS Linux release 7.4.1708 (Core) 
MW  mysql   /usr/local/mysql/bin/mysql  Ver 14.14 Distrib 5.6.40, for Linux (x86_64) using  EditLine wrapper
MW  perl    This is perl 5, version 26, subversion 2 (v5.26.2) built for x86_64-linux-thread-multi
MW  java    openjdk version "1.8.0_161"
MW  apache httpd    Server version: Apache/2.4.34 (Unix)
MW  GeoIP-1.5.0-11.el7.x86_64
MW  ImageMagick-6.7.8.9-15.el7_2.x86_64
MW  ImageMagick-devel-6.7.8.9-15.el7_2.x86_64
MW  NetworkManager-1.8.0-9.el7.x86_64
MW  NetworkManager-libnm-1.8.0-9.el7.x86_64
MW  NetworkManager-team-1.8.0-9.el7.x86_64
MW  NetworkManager-tui-1.8.0-9.el7.x86_64
MW  NetworkManager-wifi-1.8.0-9.el7.x86_64
MW  OpenEXR-libs-1.7.1-7.el7.x86_64
MW  acl-2.2.51-12.el7.x86_64
MW  aether-api-1.13.1-13.el7.noarch
<略>
MW  yum-3.4.3-154.el7.centos.noarch
MW  yum-metadata-parser-1.1.4-10.el7.x86_64
MW  yum-plugin-fastestmirror-1.1.31-42.el7.noarch
MW  zip-3.0-11.el7.x86_64
MW  zlib-1.2.7-17.el7.x86_64
MW  zlib-devel-1.2.7-17.el7.x86_64
$