Skip to content

Commit

Permalink
snmpstats: clang format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
miconda committed Dec 19, 2017
1 parent 8bc890f commit db0b73b
Show file tree
Hide file tree
Showing 40 changed files with 3,532 additions and 3,909 deletions.
67 changes: 29 additions & 38 deletions src/modules/snmpstats/alarm_checks.c
Expand Up @@ -44,19 +44,17 @@
/*! Returns the number of bytes currently waiting in the msg queue if they exceed
* the threshold, and zero otherwise. If threshold_to_compare_to is < 0, then
* no check will be performed and zero always returned. */
int check_msg_queue_alarm(int threshold_to_compare_to)
int check_msg_queue_alarm(int threshold_to_compare_to)
{
int bytesWaiting = 0;

if (threshold_to_compare_to < 0)
{
if(threshold_to_compare_to < 0) {
return 0;
}

bytesWaiting = get_total_bytes_waiting();

if (bytesWaiting > threshold_to_compare_to)
{
bytesWaiting = get_total_bytes_waiting();

if(bytesWaiting > threshold_to_compare_to) {
return bytesWaiting;
}

Expand All @@ -66,19 +64,17 @@ int check_msg_queue_alarm(int threshold_to_compare_to)

/*! Returns the number of active dialogs if they exceed the threshold, and zero
* otherwise. */
int check_dialog_alarm(int threshold_to_compare_to)
int check_dialog_alarm(int threshold_to_compare_to)
{
int num_dialogs;

if (threshold_to_compare_to < 0)
{
if(threshold_to_compare_to < 0) {
return 0;
}

num_dialogs = get_statistic("active_dialogs");

if (num_dialogs > threshold_to_compare_to)
{
if(num_dialogs > threshold_to_compare_to) {
return num_dialogs;
}

Expand All @@ -88,22 +84,21 @@ int check_dialog_alarm(int threshold_to_compare_to)
/*! This function will be called periodically from an Kamailio timer. The first
* time it is called, it will query KAMAILIO-MIB for configured thresholds.
*/
void run_alarm_check(unsigned int ticks, void * attr)
void run_alarm_check(unsigned int ticks, void *attr)
{
static int msg_queue_minor_threshold;
static int msg_queue_major_threshold;
static int msg_queue_major_threshold;

static int dialog_minor_threshold;
static int dialog_major_threshold;

static char firstRun = 1;

int bytesInMsgQueue;
int numActiveDialogs;

/* We only need to retrieve our thresholds the first time around */
if (firstRun)
{
if(firstRun) {
register_with_master_agent(ALARM_AGENT_NAME);

msg_queue_minor_threshold = get_msg_queue_minor_threshold();
Expand All @@ -114,11 +109,11 @@ void run_alarm_check(unsigned int ticks, void * attr)

firstRun = 0;
}

/* We need to have this here in case the master agent fails and is
* restarted. Without it, we won't be able to re-establish or AgentX
* connection */
agent_check_and_process(0);
agent_check_and_process(0);

/* Check for MsgQueue alarm conditions */

Expand All @@ -127,36 +122,32 @@ void run_alarm_check(unsigned int ticks, void * attr)
* of bytes will be returned. */
bytesInMsgQueue = check_msg_queue_alarm(msg_queue_minor_threshold);

if (bytesInMsgQueue != 0)
{
send_kamailioMsgQueueDepthMinorEvent_trap(bytesInMsgQueue,
msg_queue_minor_threshold);
if(bytesInMsgQueue != 0) {
send_kamailioMsgQueueDepthMinorEvent_trap(
bytesInMsgQueue, msg_queue_minor_threshold);
}

bytesInMsgQueue = check_msg_queue_alarm(msg_queue_major_threshold);


if (bytesInMsgQueue != 0)
{
send_kamailioMsgQueueDepthMajorEvent_trap(bytesInMsgQueue,
msg_queue_major_threshold);
if(bytesInMsgQueue != 0) {
send_kamailioMsgQueueDepthMajorEvent_trap(
bytesInMsgQueue, msg_queue_major_threshold);
}

/* Check for Dialog alarm conditions: */

numActiveDialogs = check_dialog_alarm(dialog_minor_threshold);
numActiveDialogs = check_dialog_alarm(dialog_minor_threshold);

if (numActiveDialogs != 0)
{
send_kamailioDialogLimitMinorEvent_trap(numActiveDialogs,
dialog_minor_threshold);
if(numActiveDialogs != 0) {
send_kamailioDialogLimitMinorEvent_trap(
numActiveDialogs, dialog_minor_threshold);
}

numActiveDialogs = check_dialog_alarm(dialog_major_threshold);

if (numActiveDialogs != 0)
{
send_kamailioDialogLimitMajorEvent_trap(numActiveDialogs,
dialog_major_threshold);
if(numActiveDialogs != 0) {
send_kamailioDialogLimitMajorEvent_trap(
numActiveDialogs, dialog_major_threshold);
}
}
}
4 changes: 2 additions & 2 deletions src/modules/snmpstats/alarm_checks.h
Expand Up @@ -34,7 +34,7 @@
#define _SNMPSTATS_ALARM_AGENT_

#define ALARM_AGENT_FREQUENCY_IN_SECONDS 5
#define ALARM_AGENT_NAME "snmpstats_alarm_agent"
#define ALARM_AGENT_NAME "snmpstats_alarm_agent"

/*! Returns the number of bytes currently waiting in the msg queue if they exceed
* the threshold, and zero otherwise. If threshold_to_compare_to is < 0, then
Expand All @@ -48,6 +48,6 @@ int check_dialog_alarm(int threshold_to_compare_to);
/*! This function will be called periodically from an Kamailio timer. The first
* time it is called, it will query KAMAILIO-MIB for configured thresholds.
*/
void run_alarm_check(unsigned int ticks, void * attr);
void run_alarm_check(unsigned int ticks, void *attr);

#endif
108 changes: 47 additions & 61 deletions src/modules/snmpstats/hashTable.c
Expand Up @@ -52,12 +52,12 @@
/*! Calculates and returns a hash index to a hash table. The index is calculated
* by summing up all the characters specified with theString, and using the
* hashTableSize as the modulus. */
int calculateHashSlot(char *theString, int hashTableSize)
int calculateHashSlot(char *theString, int hashTableSize)
{
char *currentCharacter = theString;
int runningTotal = 0;
int runningTotal = 0;

while (*currentCharacter != '\0') {
while(*currentCharacter != '\0') {
runningTotal += *currentCharacter;
currentCharacter++;
}
Expand All @@ -83,12 +83,12 @@ aorToIndexStruct_t *findHashRecord(hashSlot_t *theTable, char *aor, int size)

aorToIndexStruct_t *currentRecord = theTable[hashIndex].first;

while (currentRecord != NULL) {
while(currentRecord != NULL) {

/* If the strings are the same length and the same in every
* other way, then return the given record. */
if (currentRecord->aorLength == aorStringLength &&
memcmp(currentRecord->aor, aor, aorStringLength)==0) {
if(currentRecord->aorLength == aorStringLength
&& memcmp(currentRecord->aor, aor, aorStringLength) == 0) {
return currentRecord;
}

Expand All @@ -102,15 +102,14 @@ aorToIndexStruct_t *findHashRecord(hashSlot_t *theTable, char *aor, int size)
/*! Returns a chunk of memory large enough to store 'size' hashSlot's. The
* table will contain mappings between Kamailio's "aor" user/contact indexing
* scheme, and SNMPStats integer indexing scheme */
hashSlot_t *createHashTable(int size)
hashSlot_t *createHashTable(int size)
{
hashSlot_t *hashTable = NULL;
int numberOfBytes = sizeof(hashSlot_t)*size;
hashSlot_t *hashTable = NULL;
int numberOfBytes = sizeof(hashSlot_t) * size;

hashTable = pkg_malloc(numberOfBytes);

if (!hashTable)
{
if(!hashTable) {
LM_ERR("no more pkg memory");
return NULL;
}
Expand All @@ -122,8 +121,8 @@ hashSlot_t *createHashTable(int size)


/*! Inserts the record specified with 'theRecord' into our hash table. */
void insertHashRecord(hashSlot_t *theTable, aorToIndexStruct_t *theRecord,
int size)
void insertHashRecord(
hashSlot_t *theTable, aorToIndexStruct_t *theRecord, int size)
{
int hashIndex = calculateHashSlot(theRecord->aor, size);

Expand All @@ -133,22 +132,20 @@ void insertHashRecord(hashSlot_t *theTable, aorToIndexStruct_t *theRecord,

/* This is the first record in the hash table, so assign the first and
* last pointers to this record. */
if (theTable[hashIndex].last == NULL) {
theTable[hashIndex].last = theRecord;
if(theTable[hashIndex].last == NULL) {

theTable[hashIndex].last = theRecord;
theTable[hashIndex].first = theRecord;

} else {

/* Make the element that was previously the last element point
* to this new record, as its next element. */
theTable[hashIndex].last->next = theRecord;

/* Reassign the 'final element' pointer to this new record. */
theTable[hashIndex].last = theRecord;

}

}

/*!
Expand All @@ -165,22 +162,21 @@ void deleteUser(hashSlot_t *theTable, char *aor, int hashTableSize)
int hashIndex = calculateHashSlot(aor, hashTableSize);
int searchStringLength = strlen(aor);

aorToIndexStruct_t *currentRecord = theTable[hashIndex].first;
aorToIndexStruct_t *currentRecord = theTable[hashIndex].first;

while (currentRecord != NULL) {
while(currentRecord != NULL) {

/* First make sure both strings are the same length. If so,
* then compare all bytes. If this succeeds, then we need to
* link up the previous and next element together. */
if (currentRecord->aorLength == searchStringLength &&
memcmp(currentRecord->aor, aor, searchStringLength) == 0) {
if(currentRecord->aorLength == searchStringLength
&& memcmp(currentRecord->aor, aor, searchStringLength) == 0) {

currentRecord->numContacts--;

/* There are still contacts relying on this user, so
* don't delete anything. */
if (currentRecord->numContacts > 0)
{
if(currentRecord->numContacts > 0) {
return;
}

Expand All @@ -191,32 +187,26 @@ void deleteUser(hashSlot_t *theTable, char *aor, int hashTableSize)

/* Maintenance of the hash table */

if (currentRecord->prev == NULL)
{
/* Edge Case: First element in list was just deleted, so set
if(currentRecord->prev == NULL) {
/* Edge Case: First element in list was just deleted, so set
* up the first element to point to the one after the one
* just deleted */
theTable[hashIndex].first = currentRecord->next;
}
else
{
/* Not the first element, so hook up the previous node to
theTable[hashIndex].first = currentRecord->next;
} else {
/* Not the first element, so hook up the previous node to
* the node after the one just deleted. */
currentRecord->prev->next = currentRecord->next;
currentRecord->prev->next = currentRecord->next;
}

if (currentRecord->next == NULL)
{
/* Edge Case: The last element has been targetted for
if(currentRecord->next == NULL) {
/* Edge Case: The last element has been targetted for
* deletion. So move the pointer to the node just before
* this one. */
theTable[hashIndex].last = currentRecord->prev;
}
else
{
/* Not the last element, so hook up next nodes previous
theTable[hashIndex].last = currentRecord->prev;
} else {
/* Not the last element, so hook up next nodes previous
* element to this nodes previous. */
currentRecord->next->prev = currentRecord->prev;
currentRecord->next->prev = currentRecord->prev;
}

pkg_free(currentRecord);
Expand All @@ -228,7 +218,6 @@ void deleteUser(hashSlot_t *theTable, char *aor, int hashTableSize)
/* Advance to the next records. */
currentRecord = currentRecord->next;
}

}


Expand All @@ -240,43 +229,40 @@ void deleteUser(hashSlot_t *theTable, char *aor, int hashTableSize)
* directly to the parameter. Therefore make sure that aor is not on the stack,
* and is not going to disappear before this record is deleted.
*/
aorToIndexStruct_t *createHashRecord(int userIndex, char *aor)
aorToIndexStruct_t *createHashRecord(int userIndex, char *aor)
{
int aorLength =strlen(aor);
int aorLength = strlen(aor);

aorToIndexStruct_t *theRecord = pkg_malloc(sizeof(aorToIndexStruct_t)+
(aorLength+1)* sizeof(char));
if (theRecord == NULL)
{
aorToIndexStruct_t *theRecord = pkg_malloc(
sizeof(aorToIndexStruct_t) + (aorLength + 1) * sizeof(char));
if(theRecord == NULL) {
LM_ERR("failed to create a mapping record for %s", aor);
return NULL;
}

memset(theRecord, 0, sizeof(aorToIndexStruct_t));

theRecord->aor = (char*)theRecord + sizeof(aorToIndexStruct_t);
memcpy(theRecord->aor, aor, aorLength );
theRecord->aor = (char *)theRecord + sizeof(aorToIndexStruct_t);
memcpy(theRecord->aor, aor, aorLength);
theRecord->aor[aorLength] = '\0';
theRecord->aorLength = aorLength;
theRecord->aorLength = aorLength;
theRecord->userIndex = userIndex;
theRecord->numContacts = 1;

return theRecord;
}




/*! Debugging function. Prints off an entire hash slot. */
void printHashSlot(hashSlot_t *theTable, int index)
void printHashSlot(hashSlot_t *theTable, int index)
{
aorToIndexStruct_t *currentRecord = theTable[index].first;

LM_ERR("dumping Hash Slot #%d\n", index);

while (currentRecord != NULL) {
LM_ERR( "\tString: %s - Index: %d\n",
currentRecord->aor, currentRecord->userIndex);
while(currentRecord != NULL) {
LM_ERR("\tString: %s - Index: %d\n", currentRecord->aor,
currentRecord->userIndex);
currentRecord = currentRecord->next;
}
}
}

0 comments on commit db0b73b

Please sign in to comment.