* More license declarations
[citadel.git] / citadel / modules / mrtg / serv_mrtg.c
index 12e09f0f0ec89c96c93b5b005ae21c4ae37627d4..18d2cbc6a4867bef13149a3f1f64de477f925b5a 100644 (file)
@@ -7,6 +7,21 @@
  * is available at http://www.mrtg.org that can fetch data using external
  * scripts.  This module supplies data in the format expected by MRTG.
  *
+ * Copyright (c) 1987-2009 by the citadel.org team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "sysdep.h"
@@ -33,6 +48,7 @@
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
@@ -44,7 +60,6 @@
 #include "policy.h"
 #include "database.h"
 #include "msgbase.h"
-#include "tools.h"
 
 
 #include "ctdl_module.h"
@@ -83,6 +98,7 @@ void mrtg_users(void) {
        
        struct CitContext *cptr;
 
+       begin_critical_section(S_SESSION_TABLE);
         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
 
                if (cptr->internal_pgm == 0) {
@@ -94,7 +110,8 @@ void mrtg_users(void) {
                }
 
        }
-
+       end_critical_section(S_SESSION_TABLE);
+       
        mrtg_output(connected_users, active_users);
 }
 
@@ -107,6 +124,37 @@ void mrtg_messages(void) {
 }
 
 
+struct num_accounts {
+       long total;
+       long active;
+};
+
+/*
+ * Helper function for mrtg_accounts()
+ */
+void tally_account(struct ctdluser *EachUser, void *userdata)
+{
+       struct num_accounts *n = (struct num_accounts *) userdata;
+
+       ++n->total;
+       if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
+}
+
+
+/*
+ * Number of accounts and active accounts
+ */
+void mrtg_accounts(void) {
+       struct num_accounts n = {
+               0,
+               0
+       };
+
+       ForEachUser(tally_account, (void *)&n );
+       mrtg_output(n.total, n.active);
+}
+
+
 /*
  * Fetch data for MRTG
  */
@@ -121,17 +169,22 @@ void cmd_mrtg(char *argbuf) {
        else if (!strcasecmp(which, "messages")) {
                mrtg_messages();
        }
+       else if (!strcasecmp(which, "accounts")) {
+               mrtg_accounts();
+       }
        else {
-               cprintf("%d Unrecognized keyword '%s'\n",
-                       ERROR + ILLEGAL_VALUE, which);
+               cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
        }
 }
 
 
 CTDL_MODULE_INIT(mrtg)
 {
-        CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
-
+       if (!threading)
+       {
+               CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
+       }
+       
        /* return our Subversion id for the Log */
         return "$Id$";
 }