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