File: AccountInfo.pm

package info (click to toggle)
ocsinventory-agent 2%3A2.0.5-1.2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,120 kB
  • ctags: 899
  • sloc: perl: 20,687; sh: 576; objc: 468; ansic: 333; makefile: 55
file content (127 lines) | stat: -rw-r--r-- 3,709 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package Ocsinventory::Agent::AccountInfo;

use strict;
use warnings;

sub new {
    my (undef,$params) = @_;

    my $self = {};
    bless $self;

    $self->{config} = $params->{config};
    $self->{logger} = $params->{logger};
    $self->{common} = $params->{common};

    my $logger = $self->{logger} = $params->{logger};


    if ($self->{config}->{accountinfofile}) {

        $logger->debug ('Accountinfo file: '. $self->{config}->{accountinfofile});
        if (! -f $self->{config}->{accountinfofile}) {
            $logger->info ("Accountinfo file doesn't exist. I create an empty one.");
            $self->writeAccountInfoFile();
        } else {

            my $xmladm;

            eval {
                $xmladm = $self->{common}->readXml($self->{config}->{accountinfofile}, [ 'ACCOUNTINFO' ]);
            };


            if ($xmladm && exists($xmladm->{ACCOUNTINFO})) {
                # Store the XML content in a local HASH
                for(@{$xmladm->{ACCOUNTINFO}}){
                    if (!$_->{KEYNAME}) {
                        $logger->debug ("Incorrect KEYNAME in ACCOUNTINFO");
                    }
                    $self->{accountinfo}{ $_->{KEYNAME} } = $_->{KEYVALUE};
                }
            }
        }
    } else { 
      $logger->debug("No accountinfo file defined");
    }

    if ($self->{config}->{tag}) {
        if ($self->{accountinfo}->{TAG}) {
            $logger->debug("A TAG seems to already exist in the ocsinv.adm file. ".
                "The -t paramter will be ignored. Don't forget that the TAG value ".
                "will ignored by the server unless it has OCS_OPT_ACCEPT_TAG_UPDATE_FROM_CLIENT=1.");
        } else {
          $self->{accountinfo}->{TAG} = $self->{config}->{tag};
        }
    }
  $self; #Because we have already blessed the object 
}

# Add accountinfo stuff to an inventory
sub setAccountInfo {
    my $self = shift;
    my $inventory = shift;

    #my $ai = $self->getAll();
    $self->{xmlroot}{'CONTENT'}{ACCOUNTINFO} = [];

    my $ai = $self->{accountinfo};
    return unless $ai;

    foreach (keys %$ai) {

    push @{$inventory->{xmlroot}{'CONTENT'}{ACCOUNTINFO}}, {
            KEYNAME => [$_],
            KEYVALUE => [$ai->{$_}],
        };
    }
}

sub writeAccountInfoFile {
    my ($self, $ref) = @_;

    my $logger = $self->{logger};

    my $content;
    $content->{ACCOUNTINFO} = [];

    #We clear accountinfo to store the new one 
    undef $self->{accountinfo};

    #We get values sent by server
    if (ref ($ref) =~ /^ARRAY$/) {
        foreach (@$ref) {
            $self->{accountinfo}->{$_->{KEYNAME}} = $_->{KEYVALUE} if defined ($_->{KEYNAME}) && defined ($_->{KEYVALUE});
        }
    } elsif (ref ($ref) =~ /^HASH$/) {
        $self->{accountinfo}->{$ref->{KEYNAME}} = $ref->{KEYVALUE} if defined ($ref->{KEYNAME}) && defined ($ref->{KEYVALUE});
    } else {
        $logger->debug ("Invalid parameter while writing accountinfo file");
    }

    #We feed our XML for accountinfo file
    foreach (keys %{$self->{accountinfo}}) {
        push @{$content->{ACCOUNTINFO}}, {KEYNAME => [$_], KEYVALUE =>
            [$self->{accountinfo}{$_}]}; 
    }

    my $xml=XML::Simple::XMLout($content, RootName => 'ADM', XMLDecl=> '<?xml version="1.0" encoding="UTF-8"?>');

    #We write accountinfo file
    my $fault;
    if (!open ADM, ">".$self->{config}->{accountinfofile}) {
        $fault = 1;
    } else {
        print ADM $xml;
        $fault = 1 unless close ADM;
    }

    if (!$fault) {
        $logger->debug ("Account info updated successfully");
    } else {
        $logger->error ("Can't save account info in `".
            $self->{config}->{accountinfofile});
    }
}

1;