alarm_checks.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * SNMPStats Module 
00005  * Copyright (C) 2006 SOMA Networks, INC.
00006  * Written by: Jeffrey Magder (jmagder@somanetworks.com)
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify it
00011  * under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023  * USA
00024  *
00025  * History:
00026  * --------
00027  * 2006-11-23 initial version (jmagder)
00028  * 
00029  * This file groups together alarm checking and handling
00030  */
00031 
00039 #include <signal.h>
00040 
00041 #include "../../lib/kcore/statistics.h"
00042 
00043 #include "alarm_checks.h"
00044 #include "sub_agent.h"
00045 #include "utilities.h"
00046 #include "snmpObjects.h"
00047 #include "snmpMIBNotifications.h"
00048 
00052 int check_msg_queue_alarm(int threshold_to_compare_to) 
00053 {
00054         int bytesWaiting = 0;
00055 
00056         if (threshold_to_compare_to < 0)
00057         {
00058                 return 0;
00059         }
00060         
00061         bytesWaiting = get_total_bytes_waiting(); 
00062 
00063         if (bytesWaiting > threshold_to_compare_to)
00064         {
00065                 return bytesWaiting;
00066         }
00067 
00068         return 0;
00069 }
00070 
00071 
00074 int check_dialog_alarm(int threshold_to_compare_to) 
00075 {
00076         int num_dialogs;
00077 
00078         if (threshold_to_compare_to < 0) 
00079         {
00080                 return 0;
00081         }
00082 
00083         num_dialogs = get_statistic("active_dialogs");
00084 
00085         if (num_dialogs > threshold_to_compare_to) 
00086         {
00087                 return num_dialogs;
00088         }
00089 
00090         return 0;
00091 }
00092 
00096 void run_alarm_check(unsigned int ticks, void * attr) 
00097 {
00098         static int msg_queue_minor_threshold;
00099         static int msg_queue_major_threshold;           
00100 
00101         static int dialog_minor_threshold;
00102         static int dialog_major_threshold;
00103 
00104         static char firstRun = 1;
00105         
00106         int bytesInMsgQueue;
00107         int numActiveDialogs;
00108 
00109         /* We only need to retrieve our thresholds the first time around */
00110         if (firstRun) 
00111         {
00112                 register_with_master_agent(ALARM_AGENT_NAME);
00113 
00114                 msg_queue_minor_threshold = get_msg_queue_minor_threshold();
00115                 msg_queue_major_threshold = get_msg_queue_major_threshold();
00116 
00117                 dialog_minor_threshold = get_dialog_minor_threshold();
00118                 dialog_major_threshold = get_dialog_major_threshold();
00119 
00120                 firstRun = 0;
00121         }
00122         
00123         /* We need to have this here in case the master agent fails and is
00124          * restarted.  Without it, we won't be able to re-establish or AgentX
00125          * connection */
00126         agent_check_and_process(0); 
00127 
00128         /* Check for MsgQueue alarm conditions */
00129 
00130         /* The retrieved number of bytes will be zero unless
00131          * there is an alarm condition.  In this case the number
00132          * of bytes will be returned. */
00133         bytesInMsgQueue = check_msg_queue_alarm(msg_queue_minor_threshold);
00134 
00135         if (bytesInMsgQueue != 0) 
00136         {
00137                 send_openserMsgQueueDepthMinorEvent_trap(bytesInMsgQueue, 
00138                                                 msg_queue_minor_threshold);
00139         }
00140 
00141         bytesInMsgQueue = check_msg_queue_alarm(msg_queue_major_threshold);
00142 
00143 
00144         if (bytesInMsgQueue != 0) 
00145         {
00146                 send_openserMsgQueueDepthMajorEvent_trap(bytesInMsgQueue, 
00147                                                 msg_queue_major_threshold);
00148         }
00149 
00150         /* Check for Dialog alarm conditions: */
00151 
00152         numActiveDialogs =      check_dialog_alarm(dialog_minor_threshold);
00153 
00154         if (numActiveDialogs != 0)
00155         {
00156                 send_openserDialogLimitMinorEvent_trap(numActiveDialogs,
00157                                                 dialog_minor_threshold);
00158         }
00159         
00160         numActiveDialogs = check_dialog_alarm(dialog_major_threshold);
00161 
00162         if (numActiveDialogs != 0)
00163         {
00164                 send_openserDialogLimitMajorEvent_trap(numActiveDialogs,
00165                                                 dialog_major_threshold);
00166         }
00167 }