Advertisement
Guest User

revmon

a guest
Mar 9th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 21.44 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use POSIX qw( setuid setgid );
  7.  
  8. my %packages = (
  9.     'Fedora' => {
  10.         'Config::Simple'  => "perl-Config-Simple",
  11.     },
  12. );
  13.  
  14. loadModules();
  15.  
  16. # Define flag-variables
  17. my ( $user, $group );
  18. my ( $socket_path, $spool, $pid, $check_dir, $check_config, $cron_file, $log_file, $log_level );
  19. my ( $username, $password, $api_host, $api_key );
  20. my ( $download_checks, $auto_upgrade, $download_config, $redownload_config );
  21. my ( $new_checks, $bulk, $bulk_sleep, $bulk_pid, $daemon, $client, $help );
  22. my ( $proxy_daemon, $proxy_client, $proxy_address, $proxy_port, $proxy_pid );
  23.  
  24. # Define internal variables
  25. my ( $listener, $configuration, $config, $socket, $uid, $child_pid, $status, $check, $checks, $distro );
  26.  
  27. my $version = "0.1";
  28. my $continue = 1;
  29. $0 = "revmon";
  30.  
  31. # Load all flags
  32. GetOptions (
  33.     "u|user=s"  => \$user,
  34.     "g|group=s" => \$group,
  35.  
  36.     "S|socket=s" => \$socket_path,
  37.     "s|spool=s" => \$spool,
  38.     "p|pid=s"   => \$pid,
  39.     "e|check-dir=s" => \$check_dir,
  40.     "l|cron-file=s" => \$cron_file,
  41.     "J|check-config=s" => \$check_config,
  42.     "L|log-file=s" => \$log_file,
  43.     "V|log-level=s" => \$log_level,
  44.  
  45.     "U|username=s" => \$username,
  46.     "P|password=s" => \$password,
  47.     "A|api-host=s" => \$api_host,
  48.     "K|api-key=s" => \$api_key,
  49.  
  50.     "D|download-checks" => \$download_checks,
  51.     "a|auto-upgrade" => \$auto_upgrade,
  52.     "C|download-config" => \$download_config,
  53.     "E|redownload-config" => \$redownload_config,
  54.  
  55.     "i|new-checks" => \$new_checks,
  56.     "b|bulk" => \$bulk,
  57.     "B|bulk-sleep" => \$bulk_sleep,
  58.     "d|daemon" => \$daemon,
  59.     "c|client" => \$client,
  60.     "check=s" => \$check,
  61.  
  62.     "proxy-daemon" => \$proxy_daemon,
  63.     "proxy-client" => \$proxy_client,
  64.     "proxy-address" => \$proxy_address,
  65.     "proxy-port" => \$proxy_port,
  66.  
  67.     "help|h" => \$help,
  68.     );
  69.  
  70. if ($help) {
  71.     help();
  72. }
  73.  
  74. # Default configuration values
  75. my %default_config = (
  76.     user => 'nobody',
  77.     group => 'nogroup',
  78.     socket_path => '/var/run/run/revmon.sock',
  79.     spool => '/var/spool/revmon',
  80.     pid => '/var/run/revmon.pid',
  81.     check_dir => '/usr/share/revmon',
  82.     cron_file => '/etc/cron.d/revmon',
  83.     check_config => '/etc/revmon_checks.conf',
  84.     log_file => '/var/log/revmon.log',
  85.     log_level => '0',
  86.     download_checks => 'true',
  87.     auto_upgrade => 'true',
  88.     download_config => 'true',
  89.     bulk => 'false',
  90.     bulk_sleep => '300',
  91.     proxy_client => 'false',
  92.     proxy_daemon => 'false',
  93.     proxy_address => '127.0.0.1',
  94.     proxy_port => '6872',
  95.     );
  96.  
  97. ############################################
  98. # Here is were the program actually starts #
  99. ############################################
  100.  
  101. loadConfig();
  102. getDistro();
  103.  
  104. # Define kill-actions
  105. $SIG{TERM} = \&cleanup;
  106. $SIG{INT} = \&cleanup;
  107. $SIG{QUIT} = \&cleanup;
  108.    
  109. if ( $new_checks ) {
  110.     parseCron();
  111.     exit 0;
  112. }
  113.  
  114. getConfigs();
  115.  
  116. # Daemonize stuff
  117. if ( $daemon ) {
  118.  
  119.     pidCheck();
  120.  
  121.     # Local pids to get magic info for system users
  122.     my ($p_name, $p_pass, $p_uid, $p_gid, $p_quota, $p_comment, $p_gcos, $p_dir, $p_shell, $p_expire) = getpwnam ( $user );
  123.  
  124.     $daemon = Proc::Daemon->new('pid_file' => $pid );
  125.     $daemon->Init();
  126.        
  127.     # Fix ownership of pid-file
  128.     chown $p_uid, $p_gid, $pid;
  129.  
  130.     # No need for socket in bulk-mode
  131.     unlink( $socket_path );
  132.  
  133.     # Initilize UNIX-socket
  134.     if ($proxy_daemon ne "true" && $bulk ne "true") {
  135.     $listener = IO::Socket::UNIX->new(
  136.         Type   => SOCK_STREAM(),
  137.         Local  => $socket_path,
  138.         Listen => SOMAXCONN(),
  139.         )
  140.         or die( "Can't create server socket: $!\n" );
  141.    
  142.     # Fix permissions
  143.     chown $p_uid, $p_gid, $socket_path;
  144.  
  145.     dropPrivileges();
  146.  
  147.     # Listen for input
  148.     while( $socket = $listener->accept() ) {
  149.         chomp( my $line = <$socket> );
  150.         sendResults($line);
  151.     }
  152.     }
  153.  
  154.     # Initilize proxy
  155.     if ( $proxy_daemon eq "true") {
  156.     $proxy_daemon = Proc::Daemon->new();
  157.     $proxy_pid = $proxy_daemon->Init();
  158.    
  159.     unless ($proxy_pid) {
  160.         dropPrivileges();
  161.        
  162.         $listener = IO::Socket::INET->new(Listen    => 5,
  163.                           LocalAddr => $proxy_address,
  164.                           LocalPort => $proxy_port, #
  165.                           Proto     => 'tcp');
  166.        
  167.         while ( $socket = $listener->accept() ) {
  168.         chomp (my $line = <$socket> );
  169.         spoolWrite($line);     
  170.         }
  171.     }
  172.     }
  173.  
  174.     # Initilize bulk-mode
  175.     if ($bulk eq "true") {
  176.     $bulk = Proc::Daemon->new();
  177.     $bulk_pid = $bulk->Init();
  178.  
  179.     unless ( $bulk_pid ) {
  180.         unless ( -e $spool ) {
  181.         mkdir( $spool );
  182.         chown $p_uid, $p_gid, $spool;
  183.         }
  184.        
  185.         dropPrivileges();
  186.  
  187.         # Sleep according to bulk_sleep and call processBulk
  188.         while ( $continue ) {
  189.         sleep $bulk_sleep;
  190.         processBulk();
  191.         }
  192.     }
  193.     }
  194.  
  195.     while ( $continue ) {
  196.     # Come up with somethng fun for the master process
  197.     }
  198. }
  199.  
  200. if ( $client ) {
  201.  
  202.     dropPrivileges();
  203.  
  204.     my $line = runCheck($check);
  205.     chomp($line);
  206.     $line = time . "," . $api_key . "," . $check . "," . $line;
  207.     chomp($line);
  208.  
  209.     # Take input from check and sent to UNIX-socket or sent to proxy
  210.     if ( $line ) {
  211.     unless( $bulk eq "true" || $proxy_client eq "true") {
  212.         print "regular mode\n";
  213.         $socket = IO::Socket::UNIX->new(
  214.         Type => SOCK_STREAM(),
  215.         Peer => $socket_path,
  216.         )
  217.         or die( "Can't connect to server: $!\n" );
  218.        
  219.        
  220.         print $socket $line;
  221.     } elsif ($proxy_client eq "true") {
  222.         $socket = IO::Socket::INET->new(
  223.         PeerAddr => $proxy_address,
  224.         PeerPort => $proxy_port,
  225.         Proto => 'tcp',
  226.         );
  227.  
  228.         print $socket $line;
  229.     } else {
  230.         spoolWrite($line);
  231.     }
  232.  
  233.     exit( 0 );
  234.     }
  235. }
  236.  
  237. sub queryAuth {
  238.  
  239.     print "Username: ";
  240.     $username = <STDIN>;
  241.     chomp($username);
  242.    
  243.     print "Password: ";
  244.     system "stty -echo -icanon";
  245.     while (sysread STDIN, $a, 1) {
  246.     if (ord($a) < 32) { last; }
  247.     $password .= $a;
  248.     syswrite STDOUT, "*", 1; # print asterisk
  249.     }
  250.     system "stty echo icanon";
  251.     print "\n";
  252.    
  253.     print "Key: ";
  254.     $api_key = <STDIN>;
  255.     chomp($api_key);
  256.    
  257.     unless( $username || $password || $api_key ) {
  258.     print "You didn't fill in all fields";
  259.     exit(1);
  260.     }
  261.  
  262.     setGlobalConf($username, $password, $api_key);
  263.  
  264. }
  265.  
  266. sub setGlobalConf {
  267.  
  268.     $username = $_[0];
  269.     $password = $_[1];
  270.     $api_key = $_[2];
  271.  
  272.     open ( CFGFILE, ">>$configuration" );
  273.     print CFGFILE "username=${username}\n";
  274.     print CFGFILE "password=${password}\n";
  275.     print CFGFILE "key=${api_key}\n";
  276.  
  277.     if ( $api_host ) { print CFGFILE "api_host=" . $api_host . "\n"; }
  278.     else  { print CFGFILE "api_host=" . $default_config{'api_host'} . "\n"; }
  279.    
  280.     if ( $socket_path ) { print CFGFILE "socket_path=" . $socket_path . "\n"; }
  281.     else { print CFGFILE "socket=" . $default_config{'socket'} . "\n"; }
  282.  
  283.     if ( $spool ) { print CFGFILE "spool=" . $spool . "\n"; }
  284.     else { print CFGFILE "spool=" . $default_config{'spool'} . "\n"; }
  285.  
  286.     if ( $pid ) { print CFGFILE "pid=" . $pid . "\n"; }
  287.     else { print CFGFILE "pid=" . $default_config{'pid'} . "\n"; }
  288.  
  289.     if ( $check_dir ) { print CFGFILE "check_dir=" . $check_dir . "\n"; }
  290.     else { print CFGFILE "check_dir=" . $default_config{'check_dir'} . "\n"; }
  291.  
  292.     if ( $cron_file ) { print CFGFILE "cron_file=" . $cron_file . "\n"; }
  293.     else { print CFGFILE "cron_file=" . $default_config{'cron_file'} . "\n"; }
  294.  
  295.     if ( $check_config ) { print CFGFILE "check_config=" . $check_config . "\n"; }
  296.     else { print CFGFILE "check_config=" . $default_config{'check_config'} . "\n"; }
  297.  
  298.     if ( $log_file ) { print CFGFILE "log_file=" . $log_file . "\n"; }
  299.     else { print CFGFILE "log_file=" . $default_config{'log_file'} . "\n"; }
  300.  
  301.     if ( $log_level ) { print CFGFILE "log_level=" . $log_level . "\n"; }
  302.     else { print CFGFILE "log_level=" . $default_config{'log_level'} . "\n"; }
  303.  
  304.     if ( $download_checks ) { print CFGFILE "download_checks=false\n"; }
  305.     else { print CFGFILE "download_checks=" . $default_config{'download_checks'} . "\n"; }
  306.  
  307.     if ( $auto_upgrade ) { print CFGFILE "auto_upgrade=false\n"; }
  308.     else { print CFGFILE "auto_upgrade=" . $default_config{'auto_upgrade'} . "\n"; }
  309.  
  310.     if ( $download_config ) { print CFGFILE "download_config=false\n"; }
  311.     else { print CFGFILE "download_config=" . $default_config{'download_config'} . "\n"; }
  312.  
  313.     if ( $bulk ) {
  314.     print CFGFILE "bulk=true\n";
  315.    
  316.     if ( $bulk_sleep ) {
  317.         print CFGFILE "bulk_sleep=" . $bulk_sleep . "\n";
  318.     } else {
  319.         print CFGFILE "bulk_sleep=" . $default_config{'bulk_sleep'} . "\n";
  320.     }
  321.     }
  322.  
  323.     if ( $proxy_client) {
  324.     if ( $proxy_client ) { print CFGFILE "proxy_client=true\n"; }
  325.     else { print CFGFILE "proxy_client=" . $default_config{'proxy_client'} . "\n"; }
  326.     }
  327.  
  328.     if ( $proxy_daemon ) {
  329.     if ( $proxy_daemon ) { print CFGFILE "proxy_daemon=true\n"; }
  330.     else { print CFGFILE "proxy_daemon=" . $default_config{'proxy_daemon'} . "\n"; }
  331.    
  332.     if ( $proxy_address ) {
  333.         print CFGFILE "proxy_address=" . $proxy_address . "\n";
  334.     } else {
  335.         print CFGFILE "proxy_address=" . $default_config{'proxy_address'} . "\n";
  336.     }
  337.    
  338.     if ( $proxy_port ) {
  339.         print CFGFILE "proxy_port=" . $proxy_port . "\n";
  340.     } else {
  341.         print CFGFILE "proxy_port=" . $default_config{'proxy_port'} . "\n";
  342.     }
  343.    
  344.     }
  345.  
  346.     if ( $user ) { print CFGFILE "user=" . $user . "\n"; }
  347.     else { print CFGFILE "user=" . $default_config{'user'} . "\n"; }
  348.  
  349.     if ( $group ) { print CFGFILE "group=" . $group . "\n"; }
  350.     else { print CFGFILE "group=" . $default_config{'group'} . "\n"; }
  351.  
  352.     close ( CFGFILE );
  353.     print "\nConfig saved, please re-run $0\n";
  354.     exit(0);
  355. }
  356.  
  357. sub help {
  358.     print <<EOF;
  359. Revmon $version - 2013
  360.  
  361. Usage: $0 [OPTION]
  362. Agent for revmon service
  363.  
  364.   -u, --user              <username>               Runtime username
  365.   -g, --group             <password>               Runtime password
  366.  
  367.   -S, --socket            </var/run/revmon.sock>   Socket file path
  368.   -s, --spool             </var/spool/revmon>      Socket directory path
  369.   -p, --pid               </var/run/revmon.pid>    PID file path
  370.   -c, --check-dir         </usr/share/revmon>      Check directory
  371.   -l, --cron-file         </etc/cron.d/revmon>     CRON file
  372.   -L, --log-file          </var/log/revmon.log>    Log file
  373.  
  374.   -U, --username          <username>               API username
  375.   -P, --password          <password>               API password
  376.   -A, --api-host          <http://api.revmon.org>  API host
  377.   -K, --api-key           <key>                    API key
  378.  
  379.   -d, --download-checks   Disable automatic check downloads
  380.   -a, --auto-upgrade      Disable agent auto upgrade
  381.   -C, --download-config   Disable automatic config download
  382.   -E, --redownload-config Download new configuration
  383.  
  384.   -i, --new-checks        Check check directory for new checks
  385.   -b, --bulk              Run in bulk mode
  386.   -B, --bulk-sleep        Sleep interval for bulk-processing
  387.   -d, --daemon            Daemonize process
  388.   -c, --client            Run as client
  389.  
  390.   --proxy-daemon          Run proxy daemon
  391.   --proxy-client          Run proxy client
  392.   --proxy-address         Address to listen on
  393.   --proxy-port            Port to listen on
  394.  
  395.   -h, --help              Show this helpsection
  396. EOF
  397. exit;
  398. }
  399.  
  400. sub getConfigs {
  401.  
  402.     if ( ! -e $cron_file || $redownload_config ) {
  403.     $status = getstore( $api_host . "/" . $config->param('username').".cfg", $cron_file );
  404.     die "Unable to download cron-file $status" unless is_success($status);
  405.     }
  406.  
  407.     if ( ! -e $check_config || $redownload_config ) {
  408.     $status = getstore( $api_host . "/check_configs/" . $config->param('username').".cfg", $check_config );
  409.     die "Unable to download check-config $status" unless is_success($status);
  410.     }
  411.  
  412. }
  413.  
  414. # Parse cron
  415. sub parseCron {
  416.  
  417.     my @checks;
  418.  
  419.     open (CRON, "<" . $cron_file);
  420.     while (my $line = <CRON>) {
  421.     my @line = split(/#/, $line);
  422.     my $cron_check = $line[1];
  423.  
  424.     push ( @checks, $cron_check );
  425.     }
  426.  
  427.     checkChecks(@checks);
  428.  
  429.     close (CRON);
  430. }
  431.  
  432. # Function to handle verification of checks, downloads of new and repoting of unknowns
  433. sub checkChecks {
  434.  
  435.     map scalar $_, @_;
  436.  
  437.     # Create check_dir if not present
  438.     unless ( -e $check_dir ) {
  439.     mkdir $check_dir;
  440.     }
  441.    
  442.     # Loop all checks available and download if not present on the system
  443.     if ( @_ ) {
  444.     foreach ( @_ ) {
  445.         unless ( -e $check_dir . "/" . $_ ) {
  446.         $status = getstore( $api_host . "/checks/" . $_, $check_dir . "/" . $_);
  447.         die "Unable to download check-file $status" unless is_success($status);
  448.         }
  449.     }
  450.     }
  451.  
  452.     # Check check_dir for unknown checks and initialize them
  453.     opendir( CHECKDIR, $check_dir );
  454.     while ( my $file = readdir( CHECKDIR ) ) {
  455.     next if ($file =~ m/^\./);
  456.     if ( ! grep (/$file/, @_ ) ) {
  457.         open (TEST, ">>/tmp/test");
  458.         print TEST "Found new check $file\n";
  459.         print TEST initializeCheck($file);
  460.         close (TEST);
  461.     }
  462.     }
  463.     closedir(CHECKDIR);
  464.  
  465. }
  466.  
  467. # Execute check
  468. sub runCheck {
  469.  
  470.     $checks = new Config::Simple($check_config);
  471.  
  472.     $check = $_[0];
  473.  
  474.     my $checkresult = readpipe($check_dir . "/" . $checks->param($check));
  475.  
  476.     return $checkresult;
  477.    
  478. }
  479.  
  480. # Function to initialize checks
  481. sub initializeCheck {
  482.  
  483.     $check = $_[0];
  484.  
  485.     $check = readpipe($check_dir . "/" . $check . " -i");
  486.  
  487.     return $check;
  488. }
  489.  
  490. # Load all configuration options
  491. sub loadConfig {
  492.  
  493.     # Path to configuration
  494.     $configuration = "/etc/revmon.conf";
  495.    
  496.     # Check that config is present
  497.     unless ( -e $configuration ) {
  498.     queryAuth();
  499.     }
  500.  
  501.     # Call configuration-file
  502.     $config = new Config::Simple($configuration);
  503.    
  504.     # If value is defined by a flag, override configuration, if missing in config override by default_config
  505.     unless ( $user )            { unless ( $user = $config->param( 'user' ) )                       { $user = $default_config{'user'}; } }
  506.     unless ( $group )           { unless ( $group = $config->param( 'group' ) )                     { $group = $default_config{'group'}; } }
  507.    
  508.     unless ( $socket_path )     { unless ( $socket_path = $config->param( 'socket_path' ) )         { $socket_path = $default_config{'socket_path'}; } }
  509.     unless ( $spool )           { unless ( $spool = $config->param( 'spool' ) )                     { $spool = $default_config{'spool'}; } }
  510.     unless ( $pid )             { unless ( $pid = $config->param( 'pid' ) )                         { $pid = $default_config{'pid'}; } }
  511.     unless ( $check_dir )       { unless ( $check_dir = $config->param( 'check_dir' ) )             { $check_dir = $default_config{'check_dir'}; } }
  512.     unless ( $cron_file )       { unless ( $cron_file = $config->param( 'cron_file' ) )             { $cron_file = $default_config{'cron_file'}; } }
  513.     unless ( $check_config )    { unless ( $check_config = $config->param( 'check_config' ) )       { $check_config = $default_config{'check_config'}; } }
  514.     unless ( $log_file )        { unless ( $log_file = $config->param( 'log_file' ) )               { $log_file = $default_config{'log_file'}; } }
  515.     unless ( $log_level )       { unless ( $log_level = $config->param( 'log_level' ) )             { $log_level = $default_config{'log_level'}; } }
  516.    
  517.     unless ( $username )        { $username = $config->param( 'username' ); }
  518.     unless ( $password )        { $password = $config->param( 'password' ); }
  519.     unless ( $api_host )        { unless ( $api_host = $config->param( 'api_host' ) )               { $api_host = $default_config{'api_host'}; } }
  520.     unless ( $api_key )         { $api_key = $config->param( 'api_key' ); }
  521.    
  522.     unless ( $download_checks ) { unless ( $download_checks = $config->param( 'download_checks' ) ) { $download_checks = $default_config{'download_checks'}; } }
  523.     unless ( $auto_upgrade )    { unless ( $auto_upgrade = $config->param( 'auto_upgrade' ) )       { $auto_upgrade = $default_config{'auto_upgrade'}; } }
  524.     unless ( $download_config ) { unless ( $download_config = $config->param( 'download_config' ) ) { $download_config = $default_config{'download_config'}; } }
  525.    
  526.     unless ( $new_checks )      { unless ( $new_checks = $config->param( 'new_checks' ) )           { $new_checks = $default_config{'new_checks'}; } }
  527.     unless ( $proxy_client )    { unless ( $proxy_client = $config->param( 'proxy_client' ) )       { $proxy_client = $default_config{'proxy_client'} }; }
  528.     unless ( $proxy_daemon )    { unless ( $proxy_daemon = $config->param( 'proxy_daemon' ) )       { $proxy_daemon = $default_config{'proxy_daemon'} }; }
  529.  
  530.     # No need to define bulk_sleep unless bulk is defined
  531.     unless ( $bulk ) {
  532.     $bulk = $config->param( 'bulk' );
  533.     unless ( $bulk_sleep ) { unless ( $bulk_sleep = $config->param( 'bulk_sleep' ) ) { $bulk_sleep = $default_config{'bulk_sleep'} }; }
  534.     }
  535.  
  536.     # No need to define proxy-variables unless daemon or client is active
  537.     unless ( $proxy_daemon || $proxy_client ) {
  538.     unless ( $proxy_address ) { unless ( $proxy_address = $config->param( 'proxy_address' ) ) { $proxy_address = $default_config{'proxy_address'} }; }
  539.     unless ( $proxy_port )    { unless ( $proxy_port = $config->param( 'proxy_port' ) ) { $proxy_port = $default_config{'proxy_port'} }; }
  540.     }
  541.  
  542.     # Proxy must run as bulk
  543.     if ( $proxy_daemon eq "true" ) {
  544.     unless ( $bulk ) {
  545.         print "When running proxy-daemon you must run checks in bulk, please update your config";
  546.         exit 1;
  547.     }
  548.     }
  549.  
  550. }
  551.  
  552. # Function to drop privileges to unprivileged user
  553. sub dropPrivileges {
  554.     $uid = getpwnam($user);
  555.    
  556.     $< = $> = $uid;
  557.     if( $< != $uid ){
  558.     die "Couldn't become uid \"$uid\"\n";
  559.     }
  560.    
  561.     POSIX::setuid( $uid ) || die "Couldn't POSIX::setuid to \"$uid\" [$!]+n";
  562. }
  563.  
  564. # Process availabl results in spool
  565. sub processBulk {
  566.  
  567.     my @lines;
  568.    
  569.     opendir( SPOOL, $spool );
  570.     while ( my $file = readdir( SPOOL ) ) {
  571.     next if ($file =~ m/^\./);
  572.  
  573.     open ( SPOOLFILE, "<", $spool . "/" . $file );
  574.     push( @lines, <SPOOLFILE> );
  575.     close( SPOOLFILE );
  576.     unlink( $spool . "/" . $file );
  577.     }
  578.     closedir(SPOOL);
  579.  
  580.     if ( scalar( grep {defined $_} @lines ) > 0 ) {
  581.     sendResults(@lines);
  582.     }
  583.  
  584. }
  585.  
  586. # Sedn results to API-server
  587. sub sendResults {
  588.  
  589.     map scalar $_, @_;
  590.  
  591.     open (TEST, ">>/tmp/test");
  592.     if ( @_ ) {
  593.     foreach ( @_ ) {
  594.         print TEST $_ . "\n";
  595.     }
  596.     } else {
  597.     print TEST $_[0] . "\n";
  598.     }
  599.     close (TEST);
  600. }
  601.  
  602. # Function to verify that the API is available
  603. sub checkAPI {
  604.    
  605. }
  606.  
  607. # Clean up pids and child processes
  608. sub cleanup {
  609.     my @files = ($pid, $socket_path); unlink ( @files );  
  610.     $continue = 0;
  611.     kill 'KILL', $bulk_pid;
  612.     kill 'KILL', $proxy_pid;
  613. }
  614.  
  615. # Load modules necessary to run the agent
  616. sub loadModules {
  617.     my @install_packages;
  618.    
  619.     if ( ! eval { require Getopt::Long;1; } )     { push (@install_packages, "Getopt::Long"); }
  620.     else { Getopt::Long->import(); Getopt::Long::Configure(qw{no_auto_abbrev no_ignore_case_always}); }
  621.    
  622.     if ( ! eval { require Proc::Daemon;1; } )     { push (@install_packages, "Proc::Daemon"); }
  623.     else { Proc::Daemon->import(); }
  624.    
  625.     if ( ! eval { require Config::Simple;1; } )   { push (@install_packages, "Config::Simple"); }
  626.     else { Config::Simple->import(); }
  627.    
  628.     if ( ! eval { require LWP::UserAgent;1; } )   { push (@install_packages, "LWP::UserAgent"); }
  629.     else { LWP::UserAgent->import(); }
  630.    
  631.     if ( ! eval { require IO::Socket::UNIX;1; } ) { push (@install_packages, "IO::Socket::UNIX"); *SOCK_STREAM = sub() { die }; *SOMAXCONN = sub() { die } }
  632.     else { IO::Socket::UNIX->import( qw( SOCK_STREAM SOMAXCONN ) ); }
  633.    
  634.     if ( ! eval { require IO::Socket::INET;1; } ) { push (@install_packages, "IO::Socket::INET"); }
  635.     else { IO::Socket::INET->import(); }
  636.    
  637.     if ( ! eval { require LWP::Simple;1; } )      { push (@install_packages, "LWP::Simple"); *getstore = sub() { die }; *is_success = sub() { die } }
  638.     else { LWP::Simple->import( qw( getstore is_success ) ); }
  639.    
  640.     if ( scalar( grep {defined $_} @install_packages ) > 0 ) {
  641.     installModules(@install_packages);
  642.     }
  643. }
  644.  
  645. # Write check-results to spool
  646. sub spoolWrite {
  647.     open( SPOOLFILE, ">", "$spool/" . time );
  648.     print SPOOLFILE $_[0];
  649.     close( SPOOLFILE );
  650. }
  651.  
  652. sub pidCheck {
  653.  
  654.     if ( -e $pid ) {
  655.     open (PIDFILE, "<$pid");
  656.     my $pid_id = <PIDFILE>;
  657.     close (PIDFILE);
  658.    
  659.     if (kill 0, $pid_id) {
  660.         print "Daemon is already running\n";
  661.         exit(1);
  662.     } else {
  663.         print "Daemon isn't running but the pidfile-still exists\n";
  664.         unlink ($pid);
  665.         exit(1);
  666.     }
  667.     }
  668. }
  669.  
  670. sub logger {
  671.     open (LOGFILE, "<$log_file");
  672.     print LOGFILE $_[0];
  673.     close (LOGFILE);
  674. }
  675.  
  676. sub getDistro {
  677.     open my $file, '<', "/etc/issue";
  678.     my @distro = split(' ', <$file>);
  679.     close $file;
  680.  
  681.     return $distro[0];
  682. }
  683.  
  684. sub installModules {
  685.  
  686.     map scalar $_, @_;
  687.     my @install_packages = @_;
  688.  
  689.     $distro = getDistro();
  690.     my @install_string;
  691.     if ( $distro eq "Fedora" || $distro eq "CentOS" ) {
  692.     push( @install_string, "yum install" );
  693.     } elsif ( $distro eq "Debian" || $distro eq "Ubuntu" ) {
  694.     push( @install_string, "apt-get install" );
  695.     }
  696.    
  697.     foreach (@install_packages) {
  698.     push( @install_string, $packages{$distro}{$_} );
  699.     }
  700.    
  701.     print "You've missing dependencies, do you wish to install them? [y/n]: ";
  702.     my $answer = <STDIN>;
  703.     chomp($answer);
  704.     if ($answer eq "y" ) {
  705.     system(join(' ', @install_string));
  706.     } else {
  707.     print "You're missing " . join(',', @install_packages) . ' please run "' . join(' ', @install_string) . '" to install them'."\n";
  708.     }
  709.     exit(1);
  710. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement