|
| 1 | +# Plugin "CheckFiles" OCSInventory |
| 2 | + |
| 3 | +package Ocsinventory::Agent::Modules::Checkfiles; |
| 4 | + |
| 5 | +use POSIX qw(strftime); |
| 6 | + |
| 7 | +sub new { |
| 8 | + |
| 9 | + my $name="checkfiles"; # Name of the module |
| 10 | + |
| 11 | + my (undef,$context) = @_; |
| 12 | + my $self = {}; |
| 13 | + |
| 14 | + #Create a special logger for the module |
| 15 | + $self->{logger} = new Ocsinventory::Logger ({ |
| 16 | + config => $context->{config} |
| 17 | + }); |
| 18 | + $self->{logger}->{header}="[$name]"; |
| 19 | + $self->{context}=$context; |
| 20 | + $self->{structure}= { |
| 21 | + name => $name, |
| 22 | + start_handler => undef, #or undef if don't use this hook |
| 23 | + prolog_writer => undef, #or undef if don't use this hook |
| 24 | + prolog_reader => undef, #or undef if don't use this hook |
| 25 | + inventory_handler => $name."_inventory_handler", #or undef if don't use this hook |
| 26 | + end_handler => undef #or undef if don't use this hook |
| 27 | + }; |
| 28 | + bless $self; |
| 29 | +} |
| 30 | + |
| 31 | +######### Hook methods ############ |
| 32 | +sub checkfiles_inventory_handler { |
| 33 | + |
| 34 | + my $self = shift; |
| 35 | + my $logger = $self->{logger}; |
| 36 | + my $common = $self->{context}->{common}; |
| 37 | + |
| 38 | + |
| 39 | + $logger->debug("Yeah you are in checkfiles_inventory_handler:)"); |
| 40 | + |
| 41 | + my $datetime; |
| 42 | + my $content; |
| 43 | + # complete below variable w/ any file paths (multiple allowed) |
| 44 | + my @paths = ('/path/to/test.txt', '/path/to/test2.txt'); |
| 45 | + # set below variable to 1 for content retrieval or 0 for presence only |
| 46 | + my $getContent = 0; |
| 47 | + |
| 48 | + foreach (@paths) { |
| 49 | + $datetime = strftime('%Y-%m-%d %H:%M:%S', localtime); |
| 50 | + if (-f $_) { |
| 51 | + if ($getContent) { |
| 52 | + $logger->debug("Getting content for file : $_"); |
| 53 | + $content = do { |
| 54 | + local $/ = undef; |
| 55 | + open my $fh, "<", $_; |
| 56 | + <$fh>; |
| 57 | + }; |
| 58 | + } else { |
| 59 | + $content = 'Not retrieved'; |
| 60 | + } |
| 61 | + |
| 62 | + $exists = 'Yes'; |
| 63 | + |
| 64 | + |
| 65 | + } else { |
| 66 | + $exists = 'No'; |
| 67 | + } |
| 68 | + |
| 69 | + push @{$common->{xmltags}->{CHECKFILES}}, |
| 70 | + { |
| 71 | + PATH => [$_], |
| 72 | + CONTENT => [$content], |
| 73 | + EXIST => [$exists], |
| 74 | + LAST_CHECK => [$datetime], |
| 75 | + |
| 76 | + }; |
| 77 | + |
| 78 | + $datetime = undef; |
| 79 | + $content = undef; |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + $logger->debug("Finishing checkfiles_inventory_handler .."); |
| 86 | +} |
| 87 | + |
| 88 | +1; |
0 commit comments