diff --git a/agent/Unix/CronTab.pm b/agent/Unix/CronTab.pm new file mode 100644 index 0000000..f7838ec --- /dev/null +++ b/agent/Unix/CronTab.pm @@ -0,0 +1,131 @@ +############################################################################### +## OCSINVENTORY-NG +## Copyleft Guillaume PROTET 2010 +## Web : http://www.ocsinventory-ng.org +## +## This code is open source and may be copied and modified as long as the source +## code is always made freely available. +## Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt +################################################################################ + +package Ocsinventory::Agent::Modules::CronTab; + +use File::Find; + +sub new { + + my $name="crontab"; # Name of the module + + my (undef,$context) = @_; + my $self = {}; + + #Create a special logger for the module + $self->{logger} = new Ocsinventory::Logger ({ + config => $context->{config} + }); + + $self->{logger}->{header}="[$name]"; + + $self->{context}=$context; + + $self->{structure}= { + name => $name, + start_handler => $name."_start_handler", #or undef if don't use this hook + prolog_writer => undef, #or undef if don't use this hook + prolog_reader => undef, #or undef if don't use this hook + inventory_handler => $name."_inventory_handler", #or undef if don't use this hook + end_handler => undef #or undef if don't use this hook + }; + + bless $self; +} + +######### Hook methods ############ +sub crontab_start_handler { + my $self = shift; + my $logger = $self->{logger}; + my $common = $self->{context}->{common}; + + $logger->debug("Calling crontab_start_handler"); + + #If we cannot load prerequisite, we disable the module + unless ($common->can_load('Config::Crontab')){ + $self->{disabled} = 1; # Module is disabled + $logger->error("Config::Crontab perl module is missing !!"); + $logger->error("Humm my prerequisites are not OK... Disabling module :( :("); + } +} + +sub crontab_inventory_handler { #Use this hook to add or modify entries in the inventory XML + my $self = shift; + my $logger = $self->{logger}; + + my $common = $self->{context}->{common}; + + #I add the treatments for my new killer feature + $logger->debug("Yeah you are in crontab_inventory_handler :)"); + + #I am a killer, I get the crontabs.... + $self->{ct} = Config::Crontab->new; + find({wanted => sub { wanted($self); } }, '/etc/','/var/spool/'); + +} + +sub wanted { + my $self = shift; + my $common = $self->{context}->{common}; + my $ct = $self->{ct}; + + my @events; + my $read; + my $user; + my $command; + my $hour; + my $minute; + my $dayofmonth; + my $month; + my $dayofweek; + + return unless -f; + if ($File::Find::name =~ /cron/ && !($File::Find::name =~ m/init\.d/) && !($File::Find::name =~ m/systemd/) && !($File::Find::name =~ m/sysconfig/) && !($File::Find::name =~ m/omc/) && !($File::Find::name =~ m/pam\.d/)) { + my $fic = $File::Find::name; + + if ($fic =~ /\/var\/spool\/cron\/(.*)/) { + # Crontab file user type + $user = $1; + $user = $1 if ($user =~ /^crontabs\/(.*)/); + $ct->system(0); + } else { + $ct->system(1); + } + + # read the crontab file + $ct->read(-file => $fic); + + # Select event from crontab + @events=$ct->select( -type => 'event'); + + # Each event is read and analyzed + for (@events) { + $user = $_->{_user} unless defined($user); + $command = $_->{_command}; + $hour = $_->{_hour}; + $minute = $_->{_minute}; + $dayofmonth = $_->{_dom}; + $dayofweek = $_->{_dow}; + $month = $_->{_month}; + push @{$common->{xmltags}->{CRONTAB}}, + { + MINUTE => [$minute], + HOUR => [$hour], + DOM => [$dayofmonth], + MONTH => [$month], + DOW => [$dayofweek], + USER => [$user], + CRON => [$command], + }; + } + } +} + +1; diff --git a/agent/Unix/crontabTasks.pm b/agent/Unix/crontabTasks.pm deleted file mode 100644 index 91730b7..0000000 --- a/agent/Unix/crontabTasks.pm +++ /dev/null @@ -1,157 +0,0 @@ -############################################################################### -## OCSINVENTORY-NG -## Copyleft Guillaume PROTET 2010 -## Web : http://www.ocsinventory-ng.org -## -## This code is open source and may be copied and modified as long as the source -## code is always made freely available. -## Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt -################################################################################ - -package Ocsinventory::Agent::Modules::CronTab; - -use File::Find; -use Data::Dumper; - -sub new { - - my $name="crontab"; # Name of the module - - my (undef,$context) = @_; - my $self = {}; - - #Create a special logger for the module - $self->{logger} = new Ocsinventory::Logger ({ - config => $context->{config} - }); - - $self->{logger}->{header}="[$name]"; - - $self->{context}=$context; - - $self->{structure}= { - name => $name, - start_handler => $name."_start_handler", #or undef if don't use this hook - prolog_writer => undef, #or undef if don't use this hook - prolog_reader => undef, #or undef if don't use this hook - inventory_handler => $name."_inventory_handler", #or undef if don't use this hook - end_handler => undef #or undef if don't use this hook - }; - - bless $self; -} - -######### Hook methods ############ -sub crontab_start_handler { - my $self = shift; - my $logger = $self->{logger}; - my $common = $self->{context}->{common}; - - $logger->debug("Calling crontab_start_handler"); - - #If we cannot load prerequisite, we disable the module - unless ($common->can_load('Config::Crontab')){ - $self->{disabled}; - $logger->error("Config::Crontab perl module is missing !!"); - $logger->error("Humm my prerequisites are not OK... Disabling module :( :("); - } -} - -sub crontab_inventory_handler { #Use this hook to add or modify entries in the inventory XML - my $self = shift; - my $logger = $self->{logger}; - - my $common = $self->{context}->{common}; - - #I add the treatments for my new killer feature - $logger->debug("Yeah you are in crontab_inventory_handler :)"); - - #I am a killer, I get the crontabs.... - $self->{ct} = Config::Crontab->new; - find({wanted => sub { wanted($self); } }, '/etc/','/var/spool/'); - -} - -sub wanted { - my $self = shift; - my $common = $self->{context}->{common}; - my $ct = $self->{ct}; - - my @events; - my $read; - my $user; - my $command; - my $hour; - my $minute; - my $dayofmonth; - my $month; - my $dayofweek; - - return unless -f; - if ($File::Find::name =~ /cron/ && !($File::Find::name =~ m/init\.d/) && !($File::Find::name =~ m/systemd/) && !($File::Find::name =~ m/sysconfig/) && !($File::Find::name =~ m/omc/) && !($File::Find::name =~ m/pam\.d/)) - { - my $fic = $File::Find::name; - - if ($fic =~ /\/var\/spool\/cron\/(.*)/) - { - # Crontab file user type - $user = $1; - $user = $1 if ($user =~ /^crontabs\/(.*)/); - $ct->system(0); - } - else { $ct->system(1); } - - # read the crontab file - $ct->read(-file => $fic); - - # Select event from crontab - @events=$ct->select( -type => 'event'); - #print Dumper(@events); - - # Each event is read and analyzed - for (@events) { - $user = $_->{_user} unless defined($user); - $command = $_->{_command}; - $hour = $_->{_hour}; - $minute = $_->{_minute}; - $dayofmonth = $_->{_dom}; - $dayofweek = $_->{_dow}; - $month = $_->{_month}; - &addCrontab($common->{xmltags},{ - MINUTE => $minute, - HOUR => $hour, - DOM => $dayofmonth, - MONTH => $month, - DOW => $dayofweek, - USER => $user, - CRON => $command - }); - } - } -} - -#I create my own subroutine to add my own killing XML tags !! -sub addCrontab { - my ($xmltags,$args) = @_; - - my $ocsminute = $args->{MINUTE}; - my $ocshour = $args->{HOUR}; - my $ocsdom = $args->{DOM}; - my $ocsmonth = $args->{MONTH}; - my $ocsdow = $args->{DOW}; - my $ocsuser = $args->{USER}; - my $ocscommand = $args->{CRON}; - - push @{$xmltags->{CRONTAB}}, - { - MINUTE => [$ocsminute], - HOUR => [$ocshour], - DOM => [$ocsdom], - MONTH => [$ocsmonth], - DOW => [$ocsdow], - USER => [$ocsuser], - CRON => [$ocscommand], - }; -} - -1; \ No newline at end of file diff --git a/infos.json b/infos.json index 2e99d49..8fef059 100644 --- a/infos.json +++ b/infos.json @@ -1,7 +1,7 @@ { "displayName" : "Crontab Tasks", "author" : ["Guillaume PROTET", "Valentin DEVILLE"], - "contributor" : [], + "contributor" : ["Frank BOURDEAU"], "supportedAgent" : ["Unix"], "description" : { "fr" : "Remonte les crontab", diff --git a/install.php b/install.php index f71d3a3..ab948c3 100644 --- a/install.php +++ b/install.php @@ -2,8 +2,8 @@ function plugin_version_crontabTasks() { return array('name' => 'crontabTasks', -'version' => '1.0', -'author'=> 'Benoit SARDA, Valentin DEVILLE', +'version' => '1.1', +'author'=> 'Guillaume PROTET, Valentin DEVILLE', 'license' => 'GPLv2', 'verMinOcs' => '2.2'); } @@ -35,4 +35,4 @@ function plugin_delete_crontabTasks() $object -> del_cd_entry("crontabtasks"); $object -> sql_query("DROP TABLE `crontabtasks`"); -} \ No newline at end of file +}