| 48 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
|
|
3 |
use strict;
|
|
|
4 |
|
| 256 |
rodolico |
5 |
use version ; our $VERSION = 'v1.0.0';
|
| 48 |
rodolico |
6 |
|
| 256 |
rodolico |
7 |
# Basic FreeBSD Module for sysinfo
|
| 48 |
rodolico |
8 |
# Author: R. W. Rodolico
|
|
|
9 |
# Date: 2016-04-08
|
| 256 |
rodolico |
10 |
#
|
| 51 |
rodolico |
11 |
# gets additional systems information on BSD machine using some standard
|
| 48 |
rodolico |
12 |
# utilities
|
| 256 |
rodolico |
13 |
#
|
|
|
14 |
# Revision History
|
|
|
15 |
#
|
|
|
16 |
#
|
| 48 |
rodolico |
17 |
|
| 256 |
rodolico |
18 |
# find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script
|
|
|
19 |
# or, if run interactively, in the parent of the modules
|
| 48 |
rodolico |
20 |
BEGIN {
|
| 251 |
rodolico |
21 |
use FindBin;
|
|
|
22 |
use File::Spec;
|
| 256 |
rodolico |
23 |
# prepend the bin directory and its parent
|
|
|
24 |
use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/..");
|
|
|
25 |
eval( 'use library;' );
|
|
|
26 |
die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@;
|
| 48 |
rodolico |
27 |
}
|
|
|
28 |
|
| 256 |
rodolico |
29 |
#####
|
|
|
30 |
##### Change these to match your needs
|
|
|
31 |
#####
|
| 48 |
rodolico |
32 |
|
| 256 |
rodolico |
33 |
# Make this a list of all the modules we are going to use. You can replace undef with the version you need, if you like
|
|
|
34 |
my $modulesList = {
|
|
|
35 |
'Data::Dumper' => undef,
|
|
|
36 |
};
|
| 48 |
rodolico |
37 |
|
| 256 |
rodolico |
38 |
# hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become
|
|
|
39 |
# the full path (from which or where)
|
|
|
40 |
my $commandsList = {
|
|
|
41 |
'grep' => undef,
|
|
|
42 |
'dmesg' => undef,
|
|
|
43 |
'uname' => undef,
|
|
|
44 |
'sysctl' => undef,
|
|
|
45 |
};
|
| 48 |
rodolico |
46 |
|
| 256 |
rodolico |
47 |
# list of operating systems this module can be used on.
|
|
|
48 |
my $osList = {
|
|
|
49 |
# 'mswin32' => undef,
|
|
|
50 |
'freebsd' => undef,
|
|
|
51 |
# 'linux' => undef,
|
|
|
52 |
};
|
| 48 |
rodolico |
53 |
|
| 256 |
rodolico |
54 |
# the category the return data should go into. See sysinfo for a list
|
|
|
55 |
my $CATEGORY = 'system';
|
| 48 |
rodolico |
56 |
|
| 256 |
rodolico |
57 |
#####
|
|
|
58 |
##### End of required
|
|
|
59 |
#####
|
| 48 |
rodolico |
60 |
|
| 256 |
rodolico |
61 |
# some variables needed for our system
|
|
|
62 |
my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
|
|
|
63 |
my @out; # temporary location for each line of output
|
| 48 |
rodolico |
64 |
|
| 256 |
rodolico |
65 |
# Try to load the modules we need. If we can not, then make a list of missing modules for error message.
|
|
|
66 |
for my $module ( keys %$modulesList ) {
|
|
|
67 |
eval ( "use $module;" );
|
|
|
68 |
push @out, "$errorPrepend Could not load $module" if $@;
|
|
|
69 |
}
|
| 48 |
rodolico |
70 |
|
| 256 |
rodolico |
71 |
if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
|
|
|
72 |
push @out, "$errorPrepend Invalid Operating System";
|
|
|
73 |
}
|
|
|
74 |
if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
|
|
|
75 |
push @out, "$errorPrepend Can not find some commands needed";
|
|
|
76 |
}
|
|
|
77 |
if ( !@out ) { # we made it, we have everything, so do the processing
|
|
|
78 |
#####
|
|
|
79 |
##### Your code starts here. Remember to push all output onto @out
|
|
|
80 |
#####
|
|
|
81 |
|
|
|
82 |
my $temp = getSysctlParameter( 'sysctl', 'kern.boottime' );
|
|
|
83 |
$temp =~ m/sec = (\d+),/;
|
|
|
84 |
$temp = $1;
|
| 257 |
rodolico |
85 |
push @out, "$CATEGORY\tlast_boot\t$temp";
|
| 48 |
rodolico |
86 |
|
| 256 |
rodolico |
87 |
$temp = time - $temp;
|
| 257 |
rodolico |
88 |
push @out, "$CATEGORY\tuptime\t$temp";
|
| 256 |
rodolico |
89 |
|
|
|
90 |
#####
|
|
|
91 |
##### Your code ends here.
|
|
|
92 |
#####
|
|
|
93 |
}
|
| 55 |
rodolico |
94 |
|
| 256 |
rodolico |
95 |
# If we are testing from the command line (caller is undef), print the results for debugging
|
|
|
96 |
print join( "\n", @out ) . "\n" unless caller;
|
|
|
97 |
# called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
|
|
|
98 |
my $return = join( "\n", @out );
|
| 55 |
rodolico |
99 |
|