@@ -73,39 +73,35 @@ public function onAfterRun() {
7373 private function update_last_mail_login () {
7474 global $ app ;
7575
76- // Command to get all successful dovecot and postfix logins from the last hour.
77- $ journalCmd = "journalctl -u dovecot.service -u postfix@-.service --since='60 minutes ago' --grep '((imap|pop3)-login: Login|sasl_method=PLAIN)' " ;
76+ // used for all monitor cronjobs
77+ $ app ->load ('monitor_tools ' );
78+ $ this ->_tools = new monitor_tools ();
7879
79- $ process = popen ($ journalCmd , 'r ' );
80- if ($ process === false ) {
81- die ("Failed to execute the command " );
82- }
80+ // Get the data of the log
81+ $ log_lines = $ this ->_tools ->_getLogData ('log_mail ' , 1000000 );
8382
8483 $ updatedUsers = [];
8584
8685 // Loop over all lines.
87- while (!feof ($ process )) {
88- $ line = fgets ($ process );
89- if ($ line === false ) {
90- break ;
91- }
92-
86+ $ line = strtok ($ log_lines , PHP_EOL );
87+ while ($ line !== FALSE ) {
9388 $ matches = [];
9489 // Match pop3/imap logings, or alternately smtp logins.
95- if (preg_match ('/(.*) dovecot\[.*\]: (imap|pop3)-login: Login: user=\<([\w\.@-]+)\>/ ' , $ line , $ matches ) || preg_match ('/(.*) sasl_method=PLAIN, sasl_username=([\w\.@-]+)/ ' , $ line , $ matches )) {
90+ if (preg_match ('/(.*) (imap|pop3)-login: Login: user=\<([\w\.@-]+)\>/ ' , $ line , $ matches ) || preg_match ('/(.*) sasl_method=PLAIN, sasl_username=([\w\.@-]+)/ ' , $ line , $ matches )) {
9691 $ user = $ matches [3 ] ?? $ matches [2 ];
9792 $ updatedUsers [] = $ user ;
9893 }
99- }
10094
101- pclose ($ process );
95+ // get the next line
96+ $ line = strtok (PHP_EOL );
97+ }
10298
10399 $ uniqueUsers = array_unique ($ updatedUsers );
104100
105101 $ app ->log ('Updating last_access stats for ' . count ($ uniqueUsers ) . ' mail users ' , LOGLEVEL_DEBUG );
106102
107103 // Date/time rounded to hours.
108- $ now = time () - (time () % (60 * 60 ));
104+ $ now = time () - (time () % (60 * 60 * 24 ));
109105 $ nowFormatted = date ('Y-m-d H:i:s ' , $ now );
110106 $ sqlStatement = "UPDATE mail_user SET last_access=? WHERE email=? " ;
111107
0 commit comments