File Coverage

blib/lib/FusionInventory/Agent/Task/Inventory/Linux/Rudder.pm
Criterion Covered Total %
statement 9 38 23.6
branch 0 4 0.0
condition n/a
subroutine 3 6 50.0
pod 0 2 0.0
total 12 50 24.0


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