| 1 |
package qosient::config; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Exporter; |
| 5 |
use vars qw($VERSION @ISA @EXPORT); |
| 6 |
use Parse::DMIDecode qw(); |
| 7 |
|
| 8 |
$VERSION = "1.00"; |
| 9 |
@ISA = qw(Exporter); |
| 10 |
@EXPORT = qw(read_sys_uuid generate_sys_uuid read_os_release netconf_connect |
| 11 |
systype2name write_srcid_aliases); |
| 12 |
|
| 13 |
my $QOSIENT_SYSTEM_TYPE_COLLECTOR = 1; |
| 14 |
my $QOSIENT_SYSTEM_TYPE_SENSOR = 2; |
| 15 |
my $QOSIENT_SYSTEM_TYPE_DISTRIBUTOR = 3; |
| 16 |
|
| 17 |
my %qosient_system_type = ( |
| 18 |
'collector' => $QOSIENT_SYSTEM_TYPE_COLLECTOR, |
| 19 |
'devel' => $QOSIENT_SYSTEM_TYPE_COLLECTOR, |
| 20 |
'sensor' => $QOSIENT_SYSTEM_TYPE_SENSOR, |
| 21 |
'distributor' => $QOSIENT_SYSTEM_TYPE_DISTRIBUTOR |
| 22 |
); |
| 23 |
|
| 24 |
our %qosient_system_name = ( |
| 25 |
$QOSIENT_SYSTEM_TYPE_COLLECTOR => 'collector', |
| 26 |
$QOSIENT_SYSTEM_TYPE_SENSOR => 'sensor', |
| 27 |
$QOSIENT_SYSTEM_TYPE_DISTRIBUTOR => 'distributor' |
| 28 |
); |
| 29 |
|
| 30 |
sub systype2name { |
| 31 |
my ($systype) = @_; |
| 32 |
|
| 33 |
return $qosient_system_name{$systype}; |
| 34 |
} |
| 35 |
|
| 36 |
sub read_sys_uuid { |
| 37 |
my $filename = '/var/cache/argus/monitor-id'; |
| 38 |
my $fh; |
| 39 |
|
| 40 |
if ( !open( $fh, '<', $filename ) ) { |
| 41 |
print STDERR "Could not open file '$filename' $!\n"; |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
my $line = <$fh>; |
| 46 |
close $fh; |
| 47 |
chomp $line; |
| 48 |
return lc "$line"; |
| 49 |
} |
| 50 |
|
| 51 |
sub generate_sys_uuid { |
| 52 |
my $dmi = Parse::DMIDecode->new( nowarnings => 1 ); |
| 53 |
$dmi->probe; |
| 54 |
|
| 55 |
my $uuid = $dmi->keyword('system-uuid'); |
| 56 |
if ( !defined $uuid ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if ( $uuid =~ /^[A-F0-9]+-[A-F0-9]+-[A-F0-9]+-[A-F0-9]+-[A-F0-9]+$/ ) { |
| 61 |
return lc $uuid; |
| 62 |
} |
| 63 |
|
| 64 |
$uuid = `uuidgen -t`; |
| 65 |
if ( $? != 0 ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
return $uuid; |
| 70 |
} |
| 71 |
|
| 72 |
# href is a hash ref: srcid => name |
| 73 |
# config is a hash ref that can contain the keys: ReplaceFile, Filename |
| 74 |
# If ReplaceFile is defined and non-zero the existing file, if any, will be |
| 75 |
# overwritten. |
| 76 |
# If Filename is defined the specified name will be used instead of the |
| 77 |
# default. |
| 78 |
sub write_srcid_aliases { |
| 79 |
my ($href, $config) = @_; |
| 80 |
my $filename; |
| 81 |
my $replace_file = 0; |
| 82 |
|
| 83 |
if ( $config ) { |
| 84 |
if ( exists $config->{'ReplaceFile'} ) { |
| 85 |
$replace_file = $config->{'ReplaceFile'}; |
| 86 |
} |
| 87 |
if ( exists $config->{'Filename'} ) { |
| 88 |
$filename = $config->{'Filename'}; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if ( !defined $filename ) { |
| 93 |
$filename = '/usr/argus/srcid.alias.txt'; |
| 94 |
} |
| 95 |
|
| 96 |
if ( $replace_file == 0 && -f $filename ) { |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
my $fh; |
| 101 |
if ( !open( $fh, '>', $filename ) ) { |
| 102 |
print STDERR "Could not open file '$filename' $!\n"; |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
for my $srcid (keys %{ $href }) { |
| 107 |
print $fh "$srcid " . $href->{$srcid} . "\n"; |
| 108 |
} |
| 109 |
|
| 110 |
close $fh; |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
sub read_os_release { |
| 115 |
my $filename = '/etc/os-release'; |
| 116 |
my $fh; |
| 117 |
my $QOSIENT_SYSTEM_TYPE = $QOSIENT_SYSTEM_TYPE_COLLECTOR; |
| 118 |
|
| 119 |
if ( !open( $fh, '<', $filename ) ) { |
| 120 |
print STDERR "Could not open file '$filename' $!\n"; |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
while ( my $line = <$fh> ) { |
| 125 |
chomp $line; |
| 126 |
my ( $col1, $col2 ) = split /=/, $line; |
| 127 |
if ( !( $col1 eq 'QOSIENT_SYSTEM_TYPE' ) ) { |
| 128 |
next; |
| 129 |
} |
| 130 |
|
| 131 |
$col2 =~ s/"//g; # get rid of double-quotes |
| 132 |
my $systype = $qosient_system_type{$col2}; |
| 133 |
if ( !defined $systype ) { |
| 134 |
print STDERR "Unknown qosient system type\n"; |
| 135 |
close $fh; |
| 136 |
return; |
| 137 |
} |
| 138 |
$QOSIENT_SYSTEM_TYPE = $systype; |
| 139 |
last; |
| 140 |
} |
| 141 |
close $fh; |
| 142 |
return lc $QOSIENT_SYSTEM_TYPE; |
| 143 |
} |
| 144 |
|
| 145 |
1; |