diff --git a/modules/util/Util/Monitoring.pm b/modules/util/Util/Monitoring.pm index 5dbf5a913ef939caf165a4bbfc27771dbef04da7..be4e2e427295a2198f7d0dfac06daf5555587df6 100644 --- a/modules/util/Util/Monitoring.pm +++ b/modules/util/Util/Monitoring.pm @@ -120,8 +120,8 @@ sub read_config my $mon_cfg= Util::JSON::read_json_file($cfg_fnm); # print "mon_cfg: ", Dumper ($mon_cfg); - $obj->{'mon_cfg'}= $mon_cfg; - $obj->{'cfg_fnm'}= $cfg_fnm; + $obj->{mon_cfg}= $mon_cfg; + $obj->{cfg_fnm}= $cfg_fnm; # TODO: add mtime for update to work... # BEGIN connect to MongoDB collection @@ -158,7 +158,34 @@ sub read_config 1; } -=head setup_ref +sub get_collection +{ + my $self= shift; + my $name= shift; + + my $col_handle= '_'. $name; + return $self->{$col_handle} if (exists($self->{$col_handle})); + + my $mon_cfg= $self->{mon_cfg}; + my $paf= $mon_cfg->{'AgentDB'}; + my $col_name= setup_default_collection ($paf, $name); + + my $mdb= $self->{_mdb}; + my $col= $mdb->get_collection($col_name); + $self->{$col_handle}= $col; + $col; +} + +sub get_config +{ + my $obj= shift; + my $what= shift; + + my $res= $obj->{mon_cfg}->{$what}; + $res; +} + +=head2 setup_ref BEGIN access special settings @@ -176,7 +203,7 @@ sub setup_ref $obj->{'_auto_increment'}= \@ai; my $fs_list= $obj->{'mon_cfg'}->{'filesystems'}; - + foreach my $fs (@$fs_list) { my $mp= $fs->{'mount_point'}; @@ -189,6 +216,14 @@ sub setup_ref $ref; } +=head1 PAF methods + +=head2 $mon->event({...}) + +register an event + +=cut + sub event { my $mon= shift; @@ -198,6 +233,37 @@ sub event my $event_id= $events->insert($ev); } + +=head2 $mon->message({...}) + +send a message + +=cut + +sub send_message +{ + my $mon= shift; + my $message= shift; + my $priority= shift || 'low'; + + my $msg_cnt= 0; + my $c_msg= $mon->{_messages}; + my $notify= $mon->{mon_cfg}->{notify}; + + if (defined ($c_msg) && defined ($notify)) + { + my @notify= (ref ($notify) eq 'ARRAY') ? @$notify : $notify; + my $now= time(); + foreach my $to (@notify) + { + $c_msg->insert({ message => $message, to => $to, priority => $priority, state => 'new', 'e' => $now, 'ts' => DateTime->from_epoch('epoch' => $now),}); + $msg_cnt; + } + } + + $mon->{_events}->insert({ event => 'messages', message => $message, priority => $priority, count => $msg_cnt }); +} + =head1 FILE SYSTEM METHODS =cut @@ -307,30 +373,6 @@ sub get_machine $mon->{_machine}; } -sub send_message -{ - my $mon= shift; - my $message= shift; - my $priority= shift || 'low'; - - my $msg_cnt= 0; - my $c_msg= $mon->{_messages}; - my $notify= $mon->{mon_cfg}->{notify}; - - if (defined ($c_msg) && defined ($notify)) - { - my @notify= (ref ($notify) eq 'ARRAY') ? @$notify : $notify; - my $now= time(); - foreach my $to (@notify) - { - $c_msg->insert({ message => $message, to => $to, priority => $priority, state => 'new', 'e' => $now, 'ts' => DateTime->from_epoch('epoch' => $now),}); - $msg_cnt; - } - } - - $mon->{_events}->insert({ event => 'messages', message => $message, priority => $priority, count => $msg_cnt }); -} - =head1 SETUP FUNCTIONS =head2 $col= setup_default_collecton ($name) diff --git a/modules/util/Util/Processes.pm b/modules/util/Util/Processes.pm new file mode 100644 index 0000000000000000000000000000000000000000..66c831ae52326aaf901343ba7432ab8449bfe28a --- /dev/null +++ b/modules/util/Util/Processes.pm @@ -0,0 +1,47 @@ +=head1 NAME + + Util::Processes + +=head1 DESCRIPTION + +Deal with Unix processes + +=cut + +package Util::processes; + +sub psauxww +{ + my @cmd= qw(ps auxww); + + open (PS, '-|', @cmd) or die; + my $columns= <PS>; + chop($columns); + my @columns= split(' ', $columns); + my $column_count= 0; + my %columns= map { $_ => $column_count++ } @columns; + # print __LINE__, " columns: ", main::Dumper(\@columns); + # print __LINE__, " column_count=[$column_count]\n"; + my @processes; + while (<PS>) + { + chop; + # print __LINE__, " _=[$_]\n"; + my @f= split(' ', $_, $column_count); + my %f= map { $_ => shift (@f) } @columns; + # print __LINE__, " proc: ", main::Dumper(\%f); + push (@processes, \%f); + } + close (PS); + + my $proc= + { + columns => \@columns, + processes => \@processes, + }; + + $proc; +} + +1; +