File Coverage

blib/lib/OCS/Client.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             ## no critic (Modules::RequireExplicitPackage)
2              
3 1     1   38457 use utf8;
  1         124  
  1         7  
4 1     1   38 use strict;
  1         2  
  1         35  
5 1     1   8 use warnings;
  1         7  
  1         57  
6              
7             package OCS::Client;
8             # ABSTRACT: A simple interface to OCS's SOAP API
9             $OCS::Client::VERSION = '0.010';
10 1     1   5 use Carp;
  1         2  
  1         100  
11 1     1   1127 use URI;
  1         8354  
  1         31  
12 1     1   420 use SOAP::Lite;
  0            
  0            
13             use XML::Entities;
14             use XML::Simple;
15              
16              
17             sub new {
18             my ($class, $url, $user, $pass, @args) = @_;
19              
20             my $uri = URI->new($url);
21             $uri->path("/Apache/Ocsinventory/Interface");
22              
23             my $proxy = URI->new($url);
24              
25             my $userinfo;
26             $userinfo = $user if $user;
27             $userinfo .= ':' if $user && $pass;
28             $userinfo .= $pass if $pass;
29             $proxy->userinfo($userinfo) if $userinfo;
30              
31             $proxy->path("/ocsinterface");
32              
33             my $self = { soap => SOAP::Lite->uri($uri->as_string)->proxy($proxy->as_string, @args) };
34              
35             return bless $self, $class;
36             }
37              
38              
39             sub get_computers_V1 {
40             my ($self, @args) = @_;
41             my %request = (
42             engine => 'FIRST',
43             asking_for => 'INVENTORY',
44             checksum => 0x01FFFF,
45             wanted => 0x000003,
46             offset => 0,
47             @args,
48             );
49              
50             my $request = "\n";
51             while (my ($tag, $value) = each %request) {
52             $request .= " <\U$tag\E>$value\n";
53             }
54             $request .= "\n";
55              
56             my $som = $self->{soap}->get_computers_V1($request);
57              
58             die "ERROR: ", XML::Entities::decode('all', $som->fault->{faultstring})
59             if $som->fault;
60              
61             my @computers = $som->paramsall;
62              
63             # peel of the tag of @computers
64             shift @computers;
65             pop @computers;
66              
67             return map {XMLin($_, ForceArray => [qw/DRIVES NETWORKS PRINTERS SOFTWARES STORAGES VIDEOS/])} @computers;
68             }
69              
70              
71             sub computer_iterator {
72             my ($self, %request) = @_;
73             my @computers;
74             my $offset = 0;
75             return sub {
76             unless (@computers) {
77             @computers = $self->get_computers_V1(%request, offset => $offset);
78             ++$offset;
79             }
80             return shift @computers;
81             };
82             }
83              
84             # This hash is used to map OCS custom field ids (in the form
85             # "fields_N") into their names.
86             our %fields = (
87             3 => 'UA',
88             4 => 'Sala',
89             5 => 'Nome do Usuário',
90             6 => 'Atividade',
91             7 => 'Nome da Empresa',
92             8 => 'Ponto de Rede',
93             9 => 'Switch',
94             10 => 'Porta',
95             11 => 'Status',
96             13 => 'Observações',
97             14 => 'Local do Ponto',
98             15 => 'Asset Number',
99             16 => 'Responsável',
100             17 => 'Tipo',
101             18 => 'Padrão de HW',
102             19 => 'Data de Aquisição',
103             20 => 'UA Username',
104             21 => 'Office',
105             22 => 'Office Tag',
106             23 => 'PA',
107             26 => 'Diretoria',
108             27 => 'Nota Fiscal',
109             );
110              
111              
112             sub prune {
113             my ($computer) = @_;
114              
115             foreach (my ($key, $accountinfo) = each %{$computer->{ACCOUNTINFO}}) {
116             my %myinfo;
117             foreach my $info (grep {exists $_->{content}} @$accountinfo) {
118             if ($info->{Name} =~ /^fields_(\d+)$/) {
119             if (exists $fields{$1}) {
120             $myinfo{$fields{$1}} = $info->{content};
121             } else {
122             warn "Skipping unknown ACCOUNTINFO field id: $1";
123             }
124             } else {
125             $myinfo{$info->{Name}} = $info->{content};
126             }
127             }
128              
129             delete $myinfo{'UA Username'};
130              
131             $computer->{ACCOUNTINFO}{$key} = \%myinfo;
132             }
133              
134             if (exists $computer->{DRIVES}) {
135             foreach my $drive (@{$computer->{DRIVES}}) {
136             $drive->{ORDER} = (ref $drive->{VOLUMN} ? '' : $drive->{VOLUMN}) . (ref $drive->{LETTER} ? '' : $drive->{LETTER});
137             $drive->{ORDER} =~ s@:/$@:@;
138             delete @{$drive}{qw/CREATEDATE FREE LETTER NUMFILES VOLUMN/};
139             }
140             $computer->{DRIVES} = [sort {$a->{ORDER} cmp $b->{ORDER}} grep {$_->{TYPE} !~ /removable/i} @{$computer->{DRIVES}}];
141             }
142              
143             if (exists $computer->{HARDWARE}) {
144             delete @{$computer->{HARDWARE}}{qw/FIDELITY LASTCOME IPADDR IPSRC LASTDATE PROCESSORS QUALITY USERID SWAP/};
145             $computer->{HARDWARE}{DESCRIPTION} =~ s@^([^/]+)/\d\d-\d\d-\d\d \d\d:\d\d:\d\d$@$1@;
146             }
147              
148             if (exists $computer->{NETWORKS}) {
149             foreach my $net (@{$computer->{NETWORKS}}) {
150             delete @{$net}{qw/SPEED STATUS/};
151             }
152             }
153              
154             if (exists $computer->{PRINTERS}) {
155             $computer->{PRINTERS} = [sort {$a->{NAME} cmp $b->{NAME}} @{$computer->{PRINTERS}}];
156             }
157              
158             # Of the software we only keep the name and the version
159             if (exists $computer->{SOFTWARES}) {
160             $computer->{SOFTWARES} = {map {($_->{NAME} => $_->{VERSION})} @{$computer->{SOFTWARES}}};
161             }
162              
163             if (exists $computer->{STORAGES}) {
164             $computer->{STORAGES} = [grep {$_->{TYPE} !~ /removable/i} @{$computer->{STORAGES}}];
165             }
166              
167             if (exists $computer->{VIDEOS}) {
168             foreach my $video (@{$computer->{VIDEOS}}) {
169             delete @{$video}{qw/RESOLUTION/};
170             }
171             }
172              
173             return $computer;
174             }
175              
176              
177             use constant { ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
178             # CHECKSUM constants
179             'HARDWARE' => 0x00001,
180             'BIOS' => 0x00002,
181             'MEMORY_SLOTS' => 0x00004,
182             'SYSTEM_SLOTS' => 0x00008,
183             'REGISTRY' => 0x00010,
184             'SYSTEM_CONTROLLERS' => 0x00020,
185             'MONITORS' => 0x00040,
186             'SYSTEM_PORTS' => 0x00080,
187             'STORAGE_PERIPHERALS' => 0x00100,
188             'LOGICAL_DRIVES' => 0x00200,
189             'INPUT_DEVICES' => 0x00400,
190             'MODEMS' => 0x00800,
191             'NETWORK_ADAPTERS' => 0x01000,
192             'PRINTERS' => 0x02000,
193             'SOUND_ADAPTERS' => 0x04000,
194             'VIDEO_ADAPTERS' => 0x08000,
195             'SOFTWARE' => 0x10000,
196              
197             # WANTED constants
198             'ACOUNTINFO' => 0x00001,
199             'DICO_SOFT' => 0x00002,
200             };
201              
202             1; # End of OCS::Client
203              
204             __END__