From 49ab84b7b4fbcb6af34ebb1a0d5e5a65242f25e8 Mon Sep 17 00:00:00 2001 From: rettenbe Date: Tue, 4 Mar 2008 18:16:48 +0000 Subject: [PATCH] logged in user at client are reported periodically to server git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@9328 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/client/events/corefunctions.pm | 2 +- gosa-si/gosa-si-client | 43 ++++++++++++++++++- gosa-si/server/events/clMessages.pm | 57 +++++++++++++++++++++++--- 3 files changed, 95 insertions(+), 7 deletions(-) diff --git a/gosa-si/client/events/corefunctions.pm b/gosa-si/client/events/corefunctions.pm index 6651fcf5d..892a561f0 100644 --- a/gosa-si/client/events/corefunctions.pm +++ b/gosa-si/client/events/corefunctions.pm @@ -97,6 +97,7 @@ sub registered { if( $header eq "registered" ) { my $source = @{$msg_hash->{'source'}}[0]; &main::daemon_log("registration at $source",1); + $main::server_address = $source; } # set globaly variable client_address @@ -110,7 +111,6 @@ sub registered { unlink($main::opts_file); } - &main::daemon_log(Dumper($msg_hash),1); my $opts_file_FH; my $hostname= $main::client_dnsname; $hostname =~ s/\..*$//; diff --git a/gosa-si/gosa-si-client b/gosa-si/gosa-si-client index abda025bd..4e4845107 100755 --- a/gosa-si/gosa-si-client +++ b/gosa-si/gosa-si-client @@ -76,6 +76,9 @@ $gosa_si_client_fifo = "/var/run/gosa-si-client.socket"; my $delay_set_time = 5; our $prg= basename($0); +# all x seconds the client reports logged_in users to gosa-si-server +my $trigger_logged_in_users_report_delay = 600; + %cfg_defaults = ( "general" => {"log-file" => [\$log_file, "/var/run/".$prg.".log"], @@ -928,7 +931,34 @@ sub trigger_new_key { &send_msg_to_target($msg, $client_address, $server_key, 'new_key'); $kernel->delay_set('trigger_new_key', $server_key_lifetime); +} + + +sub trigger_logged_in_users_report { + my ($kernel) = $_[KERNEL] ; + # just do if client is registered already + if( $REGISTERED ) { + my $result = qx(/usr/bin/w -hs); + my @res_lines; + + if( defined $result ) { + chomp($result); + @res_lines = split("\n", $result); + } + + my @logged_in_user_list; + foreach my $line (@res_lines) { + chomp($line); + my @line_parts = split(/\s+/, $line); + push(@logged_in_user_list, $line_parts[0]); + } + system("echo 'CURRENTLY_LOGGED_IN ".join(" ", @logged_in_user_list)."' > /var/run/gosa-si-client.socket"); + $kernel->delay_set('trigger_logged_in_users_report', $trigger_logged_in_users_report_delay); + } else { + # try it in 1 sec again + $kernel->delay_set('trigger_logged_in_users_report', 1); + } } @@ -972,7 +1002,7 @@ sub fifo_got_record { my $clmsg_hash = &create_xml_hash("CLMSG_$header", $client_address, $server_address, $content); &add_content2xml_hash($clmsg_hash, "macaddress", $client_mac_address); my $clmsg = &create_xml_string($clmsg_hash); - &send_msg_to_target($clmsg, $server_address, $server_key); + &send_msg_to_target($clmsg, $server_address, $server_key, "CLMSG_$header"); return; } @@ -995,6 +1025,13 @@ sub _start { $heap->{services}->{ $file_watcher->ID } = $file_name; $heap->{watchers}->{ $file_watcher->ID } = $file_watcher; } + $kernel->yield('trigger_logged_in_users_report'); +} + + +sub _default { + daemon_log("ERROR: can not handle incoming msg with header '$_[ARG0]'", 1); + return; } @@ -1199,8 +1236,12 @@ POSIX::mkfifo("$gosa_si_client_fifo", "0600"); POE::Session->create( inline_states => { _start => \&_start, + _default => \&_default, register_at_gosa_si_server => \®ister_at_gosa_si_server, + + # trigger periodical tasks trigger_new_key => \&trigger_new_key, + trigger_logged_in_users_report => \&trigger_logged_in_users_report, # handle records from each defined file differently fifo_record => \&fifo_got_record, diff --git a/gosa-si/server/events/clMessages.pm b/gosa-si/server/events/clMessages.pm index a7487a3e1..088ce5ab7 100644 --- a/gosa-si/server/events/clMessages.pm +++ b/gosa-si/server/events/clMessages.pm @@ -12,6 +12,7 @@ my @events = ( "GOTOACTIVATION", "LOGIN", "LOGOUT", + "CURRENTLY_LOGGED_IN", ); @EXPORT = @events; @@ -82,7 +83,7 @@ sub LOGIN { client=>$source, user=>$login, timestamp=>&get_time, - ); + ); my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash ); if ($res != 0) { &main::daemon_log("ERROR: cannot add entry to known_clients: $error_str"); @@ -92,7 +93,7 @@ sub LOGIN { return; } - +# TODO umstellen wie bei LOGIN sub LOGOUT { my ($msg, $msg_hash, $session_id) = @_; my $header = @{$msg_hash->{'header'}}[0]; @@ -111,15 +112,61 @@ sub LOGOUT { if( $act_login eq "" ){ $act_login = "nobody"; } - $sql_statement = "UPDATE known_clients ". - "SET login='$act_login' ". - "WHERE hostname='$source'"; + $sql_statement = "UPDATE known_clients SET login='$act_login' WHERE hostname='$source'"; $res = $main::known_clients_db->update_dbentry($sql_statement); return; } +sub CURRENTLY_LOGGED_IN { + my ($msg, $msg_hash, $session_id) = @_; + my ($sql_statement, $db_res); + my $header = @{$msg_hash->{'header'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + my $login = @{$msg_hash->{$header}}[0]; + + $sql_statement = "SELECT * FROM $main::login_users_tn WHERE client='$source'"; + $db_res = $main::login_users_db->select_dbentry($sql_statement); + my %currently_logged_in_user = (); + while( my($hit_id, $hit) = each(%{$db_res}) ) { + $currently_logged_in_user{$hit->{'user'}} = 1; + } + &main::daemon_log("DEBUG: logged in users from login_user_db: ".join(", ", keys(%currently_logged_in_user)), 7); + + my @logged_in_user = split(/\s+/, $login); + &main::daemon_log("DEBUG: logged in users reported from client: ".join(", ", @logged_in_user), 7); + foreach my $user (@logged_in_user) { + my %add_hash = ( table=>$main::login_users_tn, + primkey=> ['client', 'user'], + client=>$source, + user=>$user, + timestamp=>&get_time, + ); + my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash ); + if ($res != 0) { + &main::daemon_log("ERROR: cannot add entry to known_clients: $error_str"); + return; + } + + delete $currently_logged_in_user{$user}; + } + + # if there is still a user in %currently_logged_in_user + # although he is not reported by client + # then delete it from $login_user_db + foreach my $obsolete_user (keys(%currently_logged_in_user)) { + &main::daemon_log("WARNING: user '$obsolete_user' is currently not logged ". + "in at client '$source' but still found at login_user_db", 3); + my $sql_statement = "DELETE FROM $main::login_users_tn WHERE client='$source' AND user='$obsolete_user'"; + my $res = $main::login_users_db->del_dbentry($sql_statement); + &main::daemon_log("WARNING: delete user '$obsolete_user' at client '$source' from login_user_db", 3); + } + + return; +} + + sub GOTOACTIVATION { my ($msg, $msg_hash, $session_id) = @_; my $out_msg = &build_result_update_msg($msg_hash); -- 2.30.2