File Coverage

blib/lib/FusionInventory/Agent/Task/Inventory/Generic/Rudder.pm
Criterion Covered Total %
statement 12 42 28.5
branch 0 6 0.0
condition n/a
subroutine 4 7 57.1
pod 0 2 0.0
total 16 57 28.0


line stmt bran cond sub pod time code
1             package FusionInventory::Agent::Task::Inventory::Generic::Rudder;
2              
3 1     1   129285664 use strict;
  1         3  
  1         96  
4 1     1   6 use warnings;
  1         15  
  1         106  
5              
6 1     1   11 use English qw(-no_match_vars);
  1         97  
  1         25  
7              
8 1     1   1922 use FusionInventory::Agent::Tools;
  1         3  
  1         572  
9              
10             sub isEnabled {
11 0     0 0   return -r '/opt/rudder/etc/uuid.hive';
12             }
13              
14             sub doInventory {
15 0     0 0   my (%params) = @_;
16              
17 0           my $inventory = $params{inventory};
18 0           my $logger = $params{logger};
19              
20             # get Rudder UUID
21 0           my $Uuid = getFirstLine(
22             logger => $logger, file => '/opt/rudder/etc/uuid.hive'
23             );
24             # get all agents running on that machine
25 0           my @agents = _manageAgent(
26             logger => $logger, command => 'ls /var/rudder'
27             );
28             # get machine hostname
29 0 0         my $command = $OSNAME eq 'linux' ? 'hostname --fqd' : 'hostname';
30 0           my $hostname = getFirstLine(
31             logger => $logger, command => $command
32             );
33 0           my $rudder = {
34             HOSTNAME => $hostname,
35             UUID => $Uuid,
36             AGENT => \@agents,
37             };
38              
39 0           $inventory->addEntry(
40             section => 'RUDDER', entry => $rudder
41             );
42             }
43              
44             sub _manageAgent {
45 0     0     my $handle = getFileHandle(@_);
46 0           my %params = @_;
47 0           my $logger = $params{logger};
48              
49 0           my @agents;
50              
51             # each line could be a new agent
52 0           while(my $name = <$handle>){
53              
54 0           chomp $name;
55             # verify agent name
56 0 0         next unless $name =~ /cfengine/;
57              
58 0           my $server_hostname_file = "/var/rudder/$name/policy_server.dat";
59 0           my $uuid_file = "/var/rudder/$name/rudder-server-uuid.txt";
60 0           my $cfengine_key_file = "/var/rudder/$name/ppkeys/localhost.pub";
61              
62             # get policy server hostname
63 0           my $serverHostname = getFirstLine (
64             logger => $logger,
65             file => $server_hostname_file
66             );
67 0           chomp $serverHostname;
68              
69             # get policy server uuid
70             #
71             # the default file is no longer /var/rudder/tmp/uuid.txt since the
72             # change in http://www.rudder-project.org/redmine/issues/2443.
73             # we gracefully fallback to the old default if we can not find the
74             # new file.
75 0 0         my $serverUuid = getFirstLine (
76             logger => $logger,
77             file => ( -e "$uuid_file" ) ? $uuid_file : "/var/rudder/tmp/uuid.txt"
78             );
79 0           chomp $serverUuid;
80              
81             # get CFengine public key
82 0           my $cfengineKey = getAllLines(
83             file => $cfengine_key_file
84             );
85              
86             # get owner name
87 0           my $owner = getFirstLine (
88             logger => $logger,
89             command => 'whoami'
90             );
91              
92             # build agent from datas
93 0           my $agent = {
94             AGENT_NAME => $name,
95             POLICY_SERVER_HOSTNAME => $serverHostname,
96             CFENGINE_KEY => $cfengineKey,
97             OWNER => $owner,
98             POLICY_SERVER_UUID => $serverUuid,
99             };
100              
101 0           push @agents, $agent;
102              
103             }
104              
105 0           close $handle;
106 0           return @agents;
107             }
108              
109             1;