File Coverage

lib/Rex/Commands/Gather.pm
Criterion Covered Total %
statement 59 122 48.3
branch 14 56 25.0
condition 6 17 35.2
subroutine 21 38 55.2
pod 27 27 100.0
total 127 260 48.8


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::Commands::Gather - Hardware and Information gathering
8              
9             =head1 DESCRIPTION
10              
11             With this module you can gather hardware and software information.
12              
13             All these functions will not be reported. These functions don't modify anything.
14              
15             =head1 SYNOPSIS
16              
17             operating_system_is("SuSE");
18              
19              
20             =head1 EXPORTED FUNCTIONS
21              
22             =cut
23              
24             package Rex::Commands::Gather;
25              
26 36     36   468 use v5.12.5;
  36         132  
27 36     36   191 use warnings;
  36         101  
  36         1591  
28              
29             our $VERSION = '1.14.3'; # VERSION
30              
31 36     36   318 use Data::Dumper;
  36         69  
  36         1836  
32 36     36   463 use Rex::Hardware;
  36         82  
  36         629  
33 36     36   1210 use Rex::Hardware::Host;
  36         106  
  36         264  
34 36     36   1402 use Rex::Hardware::Network;
  36         114  
  36         408  
35 36     36   1216 use Rex::Hardware::Memory;
  36         93  
  36         398  
36 36     36   1170 use Rex::Helper::System;
  36         89  
  36         267  
37 36     36   782 use Rex::Commands;
  36         84  
  36         228  
38              
39             require Rex::Exporter;
40 36     36   292 use base qw(Rex::Exporter);
  36         96  
  36         2523  
41              
42 36     36   230 use vars qw(@EXPORT);
  36         73  
  36         58170  
43              
44             @EXPORT = qw(operating_system_is network_interfaces memory
45             get_operating_system operating_system operating_system_version operating_system_release
46             is_freebsd is_netbsd is_openbsd is_redhat is_linux is_bsd is_solaris is_suse is_debian is_mageia is_windows is_alt is_openwrt is_gentoo is_fedora is_arch is_void
47             get_system_information dump_system_information kernelname);
48              
49             =head2 get_operating_system
50              
51             Will return the current operating system name.
52              
53             task "get-os", "server01", sub {
54             say get_operating_system();
55             };
56              
57             Aliased by operating_system().
58              
59             =cut
60              
61             sub get_operating_system {
62              
63 19     19 1 336 my $operatingsystem = Rex::Hardware::Host->get_operating_system();
64              
65 19   50     344 return $operatingsystem || "unknown";
66              
67             }
68              
69             =head2 operating_system
70              
71             Alias for get_operating_system()
72              
73             =cut
74              
75             sub operating_system {
76 0     0 1 0 return get_operating_system();
77             }
78              
79             =head2 get_system_information
80              
81             Will return a hash of all system information. These Information will be also used by the template function.
82              
83             =cut
84              
85             sub get_system_information {
86 0     0 1 0 return Rex::Helper::System::info();
87             }
88              
89             =head2 kernelname
90              
91             Will return the kernel name of the operating system. For example on a linux system it will return I.
92              
93             =cut
94              
95             sub kernelname {
96 0     0 1 0 my $host = Rex::Hardware::Host->get();
97 0         0 return $host->{kernelname};
98             }
99              
100             =head2 dump_system_information
101              
102             This function dumps all known system information on stdout.
103              
104             =cut
105              
106             sub dump_system_information {
107 0     0 1 0 my ($info) = @_;
108 0         0 my %sys_info = get_system_information();
109              
110 0         0 inspect( \%sys_info,
111             { prepend_key => '$', key_value_sep => " = ", no_root => 1 } );
112             }
113              
114             =head2 operating_system_is($string)
115              
116             Will return 1 if the operating system is $string.
117              
118             task "is_it_suse", "server01", sub {
119             if( operating_system_is("SuSE") ) {
120             say "This is a SuSE system.";
121             }
122             };
123              
124             =cut
125              
126             sub operating_system_is {
127              
128 4     4 1 26 my (@oses) = @_;
129              
130 4         63 my $operatingsystem = Rex::Hardware::Host->get_operating_system();
131              
132 4         37 for my $os (@oses) {
133 4 100       31 if ( $operatingsystem eq $os ) {
134 2         21 return 1;
135             }
136             }
137              
138 2         80 return 0;
139              
140             }
141              
142             =head2 operating_system_version()
143              
144             Will return the os release number as an integer. For example, it will convert 5.10 to 510, 10.04 to 1004 or 6.0.3 to 603.
145              
146             task "prepare", "server01", sub {
147             if( operating_system_version() >= 510 ) {
148             say "OS Release is higher or equal to 510";
149             }
150             };
151              
152             =cut
153              
154             sub operating_system_version {
155              
156 0     0 1 0 my ($os) = @_;
157              
158 0         0 my $operatingsystemrelease = operating_system_release();
159              
160 0         0 $operatingsystemrelease =~ s/[\.,]//g;
161              
162 0         0 return $operatingsystemrelease;
163              
164             }
165              
166             =head2 operating_system_release()
167              
168             Will return the os release number as is.
169              
170             =cut
171              
172             sub operating_system_release {
173 0     0 1 0 my ($os) = @_;
174 0         0 return Rex::Hardware::Host->get_operating_system_version();
175             }
176              
177             =head2 network_interfaces
178              
179             Return an HashRef of all the networkinterfaces and their configuration.
180              
181             task "get_network_information", "server01", sub {
182             my $net_info = network_interfaces();
183             };
184              
185             You can iterate over the devices as follow
186              
187             my $net_info = network_interfaces();
188             for my $dev ( keys %{ $net_info } ) {
189             say "$dev has the ip: " . $net_info->{$dev}->{"ip"} . " and the netmask: " . $net_info->{$dev}->{"netmask"};
190             }
191              
192             =cut
193              
194             sub network_interfaces {
195              
196 0     0 1 0 my $net = Rex::Hardware::Network->get();
197              
198 0         0 return $net->{"networkconfiguration"};
199              
200             }
201              
202             =head2 memory
203              
204             Return an HashRef of all memory information.
205              
206             task "get_memory_information", "server01", sub {
207             my $memory = memory();
208              
209             say "Total: " . $memory->{"total"};
210             say "Free: " . $memory->{"free"};
211             say "Used: " . $memory->{"used"};
212             say "Cached: " . $memory->{"cached"};
213             say "Buffers: " . $memory->{"buffers"};
214             };
215              
216             =cut
217              
218             sub memory {
219              
220 0     0 1 0 my $mem = Rex::Hardware::Memory->get();
221              
222 0         0 return $mem;
223              
224             }
225              
226             =head2 is_freebsd
227              
228             Returns true if the target system is a FreeBSD.
229              
230             task "foo", "server1", "server2", sub {
231             if(is_freebsd) {
232             say "This is a freebsd system...";
233             }
234             else {
235             say "This is not a freebsd system...";
236             }
237             };
238              
239             =cut
240              
241             sub is_freebsd {
242 2 50   2 1 12 my $os = @_ ? shift : get_operating_system();
243 2 50       28 if ( $os =~ m/FreeBSD/i ) {
244 0         0 return 1;
245             }
246             }
247              
248             =head2 is_redhat
249              
250             task "foo", "server1", sub {
251             if(is_redhat) {
252             # do something on a redhat system (like RHEL, Fedora, CentOS, Scientific Linux
253             }
254             };
255              
256             =cut
257              
258             sub is_redhat {
259 2 50   2 1 12 my $os = @_ ? shift : get_operating_system();
260              
261 2         13 my @redhat_clones = (
262             "Fedora", "Redhat",
263             "CentOS", "Scientific",
264             "RedHatEnterpriseServer", "RedHatEnterpriseES",
265             "RedHatEnterpriseWorkstation", "RedHatEnterpriseWS",
266             "Amazon", "ROSAEnterpriseServer",
267             "CloudLinuxServer", "XenServer",
268             "OracleServer", "Virtuozzo",
269             );
270              
271 2 50       5 if ( grep { /$os/i } @redhat_clones ) {
  28         75  
272 0         0 return 1;
273             }
274             }
275              
276             =head2 is_fedora
277              
278             task "foo", "server1", sub {
279             if(is_fedora) {
280             # do something on a fedora system
281             }
282             };
283              
284             =cut
285              
286             sub is_fedora {
287 0 0   0 1 0 my $os = @_ ? shift : get_operating_system();
288              
289 0         0 my @fedora_clones = ("Fedora");
290              
291 0 0       0 if ( grep { /$os/i } @fedora_clones ) {
  0         0  
292 0         0 return 1;
293             }
294             }
295              
296             =head2 is_suse
297              
298             task "foo", "server1", sub {
299             if(is_suse) {
300             # do something on a suse system
301             }
302             };
303              
304             =cut
305              
306             sub is_suse {
307 0 0   0 1 0 my $os = @_ ? shift : get_operating_system();
308              
309 0         0 my @suse_clones = ( "OpenSuSE", "SuSE", "openSUSE project" );
310              
311 0 0       0 if ( grep { /$os/i } @suse_clones ) {
  0         0  
312 0         0 return 1;
313             }
314             }
315              
316             =head2 is_mageia
317              
318             task "foo", "server1", sub {
319             if(is_mageia) {
320             # do something on a mageia system (or other Mandriva followers)
321             }
322             };
323              
324             =cut
325              
326             sub is_mageia {
327 0 0   0 1 0 my $os = @_ ? shift : get_operating_system();
328              
329 0         0 my @mdv_clones = ( "Mageia", "ROSADesktopFresh" );
330              
331 0 0       0 if ( grep { /$os/i } @mdv_clones ) {
  0         0  
332 0         0 return 1;
333             }
334             }
335              
336             =head2 is_debian
337              
338             task "foo", "server1", sub {
339             if(is_debian) {
340             # do something on a debian system
341             }
342             };
343              
344             =cut
345              
346             sub is_debian {
347 0 0   0 1 0 my $os = @_ ? shift : get_operating_system();
348              
349 0         0 my @debian_clones = ( "Debian", "Ubuntu", "LinuxMint", "Raspbian", "Devuan" );
350              
351 0 0       0 if ( grep { /$os/i } @debian_clones ) {
  0         0  
352 0         0 return 1;
353             }
354             }
355              
356             =head2 is_alt
357              
358             task "foo", "server1", sub {
359             if(is_alt) {
360             # do something on a ALT Linux system
361             }
362             };
363              
364             =cut
365              
366             sub is_alt {
367 0 0   0 1 0 my $os = @_ ? shift : get_operating_system();
368              
369 0         0 my @alt_clones = ("ALT");
370              
371 0 0       0 if ( grep { /$os/i } @alt_clones ) {
  0         0  
372 0         0 return 1;
373             }
374             }
375              
376             =head2 is_netbsd
377              
378             Returns true if the target system is a NetBSD.
379              
380             task "foo", "server1", "server2", sub {
381             if(is_netbsd) {
382             say "This is a netbsd system...";
383             }
384             else {
385             say "This is not a netbsd system...";
386             }
387             };
388              
389             =cut
390              
391             sub is_netbsd {
392 2 50   2 1 15 my $os = @_ ? shift : get_operating_system();
393 2 50       76 if ( $os =~ m/NetBSD/i ) {
394 0         0 return 1;
395             }
396             }
397              
398             =head2 is_openbsd
399              
400             Returns true if the target system is an OpenBSD.
401              
402             task "foo", "server1", "server2", sub {
403             if(is_openbsd) {
404             say "This is an openbsd system...";
405             }
406             else {
407             say "This is not an openbsd system...";
408             }
409             };
410              
411             =cut
412              
413             sub is_openbsd {
414 2 50   2 1 15 my $os = @_ ? shift : get_operating_system();
415 2 50       50 if ( $os =~ m/OpenBSD/i ) {
416 0         0 return 1;
417             }
418             }
419              
420             =head2 is_linux
421              
422             Returns true if the target system is a Linux System.
423              
424             task "prepare", "server1", "server2", sub {
425             if(is_linux) {
426             say "This is a linux system...";
427             }
428             else {
429             say "This is not a linux system...";
430             }
431             };
432              
433             =cut
434              
435             sub is_linux {
436              
437 9     9 1 213 my $host = Rex::Hardware::Host->get();
438 9 50 33     486 if ( exists $host->{kernelname}
      33        
439             && $host->{kernelname}
440             && $host->{kernelname} =~ m/Linux/ )
441             {
442 9         264 return 1;
443             }
444             }
445              
446             =head2 is_bsd
447              
448             Returns true if the target system is a BSD System.
449              
450             task "prepare", "server1", "server2", sub {
451             if(is_bsd) {
452             say "This is a BSD system...";
453             }
454             else {
455             say "This is not a BSD system...";
456             }
457             };
458              
459             =cut
460              
461             sub is_bsd {
462              
463 0     0 1 0 my $host = Rex::Hardware::Host->get();
464 0 0       0 if ( $host->{"kernelname"} =~ m/BSD/ ) {
465 0         0 return 1;
466             }
467             }
468              
469             =head2 is_solaris
470              
471             Returns true if the target system is a Solaris System.
472              
473             task "prepare", "server1", "server2", sub {
474             if(is_solaris) {
475             say "This is a Solaris system...";
476             }
477             else {
478             say "This is not a Solaris system...";
479             }
480             };
481              
482             =cut
483              
484             sub is_solaris {
485              
486 9     9 1 677 my $host = Rex::Hardware::Host->get();
487 9 50 33     575 if ( exists $host->{kernelname}
      33        
488             && $host->{kernelname}
489             && $host->{"kernelname"} =~ m/SunOS/ )
490             {
491 0         0 return 1;
492             }
493             }
494              
495             =head2 is_windows
496              
497             Returns true if the target system is a Windows System.
498              
499             =cut
500              
501             sub is_windows {
502              
503 1     1 1 59 my $host = Rex::Hardware::Host->get();
504 1 50 33     96 if ( $host->{"operatingsystem"} =~ m/^MSWin/
505             || $host->{operatingsystem} eq "Windows" )
506             {
507 0         0 return 1;
508             }
509              
510             }
511              
512             =head2 is_openwrt
513              
514             Returns true if the target system is an OpenWrt System.
515              
516             =cut
517              
518             sub is_openwrt {
519 2     2 1 25 my $os = get_operating_system();
520 2 50       52 if ( $os =~ m/OpenWrt/i ) {
521 0           return 1;
522             }
523              
524             }
525              
526             =head2 is_gentoo
527              
528             Returns true if the target system is a Gentoo System.
529              
530             =cut
531              
532             sub is_gentoo {
533 0     0 1   my $os = get_operating_system();
534 0 0         if ( $os =~ m/Gentoo/i ) {
535 0           return 1;
536             }
537              
538             }
539              
540             =head2 is_arch
541              
542             task "foo", "server1", sub {
543             if(is_arch) {
544             # do something on a arch system
545             }
546             };
547              
548             =cut
549              
550             sub is_arch {
551 0 0   0 1   my $os = @_ ? shift : get_operating_system();
552              
553 0           my @arch_clones = ( "Arch", "Manjaro", "Antergos" );
554              
555 0 0         if ( grep { /$os/i } @arch_clones ) {
  0            
556 0           return 1;
557             }
558             }
559              
560             =head2 is_void
561              
562             Returns true if the target system is a Void Linux system.
563              
564             =cut
565              
566             sub is_void {
567 0     0 1   my $os = get_operating_system();
568 0 0         if ( $os =~ m/VoidLinux/i ) {
569 0           return 1;
570             }
571              
572             }
573              
574             1;