Icinga-core 1.4.0
next gen monitoring
base/statsprofiler.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * STATSPROFILER.C
00004  *
00005  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)
00006  *
00007  * License:
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License version 2 as
00011  * published by the Free Software Foundation.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021  *
00022  *****************************************************************************/
00023 
00024 #include "../include/config.h"
00025 #include "../include/common.h"
00026 #include "../include/icinga.h"
00027 #include "../include/locations.h"
00028 
00029 /* make sure gcc3 won't hit here */
00030 #ifndef GCCTOOOLD
00031 
00032 #include "../include/statsprofiler.h"
00033 
00034 extern profile_object* profiled_data;
00035 
00036 double safe_divide(double x, int y, int reverse)
00037 {
00038 
00039         double ans;
00040         if(x == 0 || y ==0)
00041         {
00042                 ans = 0.0f;
00043         }else{
00044                 if(reverse)
00045                 {
00046                         ans = y/x;
00047                 }else{
00048                         ans = x/y;
00049                 }
00050         }
00051         return ans;
00052 }
00053 
00054 profile_object* profiled_data_find_last_object()
00055 {
00056         profile_object* p = profiled_data;
00057         if(p)
00058         {
00059                 while(p->next && p->next != p)
00060                         p = p->next;
00061         }
00062         return p;
00063 }
00064 
00065 profile_object* profile_object_create(char * name)
00066 {
00067         profile_object* new_p =  (profile_object*) calloc(1,sizeof(profile_object));
00068         profile_object* old_p = profiled_data_find_last_object();
00069 
00070         if(!old_p)
00071         {
00072                 profiled_data = old_p = new_p;
00073         }
00074 
00075         if(new_p != old_p)
00076                 old_p->next = new_p;
00077 
00078         new_p->name = strdup(name);
00079         return new_p;
00080 }
00081 
00082 profile_object* profile_object_find_by_name(char * name)
00083 {
00084         profile_object* p = profiled_data;
00085         char * n_name = calloc(strlen(name)+1,sizeof(char));
00086         strncpy(n_name,name,strlen(name));
00087 
00088         while(p != NULL)
00089         {
00090                 if(strcmp(n_name,p->name)==0)
00091                 {
00092                         break;
00093                 }
00094                 p = p->next;
00095         }
00096 
00097         if(!p)
00098         {
00099                 p = profile_object_create(n_name);
00100         }
00101         free(n_name);
00102         return p;
00103 }
00104 
00105 void profile_object_update_count(char * name, int val)
00106 {
00107         profile_object* new_p = profile_object_find_by_name(name);
00108         new_p->count=val;
00109 }
00110 
00111 void profile_object_update_elapsed(char * name, double val)
00112 {
00113         profile_object * new_p = profile_object_find_by_name(name);
00114         new_p->elapsed=val;
00115 }
00116 
00117 void profile_data_print()
00118 {
00119         int count;
00120         char * name;
00121         double elapsed;
00122         double total_time;
00123         profile_object *t, *p = profiled_data;
00124         t=profile_object_find_by_name("EVENT_LOOP_COMPLETION");
00125         total_time = t->elapsed;
00126 
00127         while(p)
00128         {
00129                 name = p->name;
00130                 count = p->count;
00131                 elapsed = p->elapsed;
00132 
00133                 printf("%s\t\t\t%.3f / %d / %.4f / %.4f\n",name,elapsed,count,safe_divide(elapsed,count,0),safe_divide(total_time,count,1));
00134                 p = p->next;
00135         }
00136 }
00137 
00138 void profile_data_output_mrtg(char * name, char * delim)
00139 {
00140         int t_len = strlen(name);
00141         char t_name[t_len];
00142         profile_object* p = profile_object_find_by_name(t_name);
00143         profile_object* t = profile_object_find_by_name("EVENT_LOOP_COMPLETION");
00144         
00145         strncpy(t_name,name+strlen("COUNTER_"),t_len);
00146 
00147         if(strstr(name,"ELAPSED"))
00148                 printf("%.3f%s",p->elapsed,delim);
00149 
00150         if(strstr(name,"COUNTER"))
00151                 printf("%d%s",p->count,delim);
00152 
00153         if(strstr(name,"EVENTPS"))
00154                 printf("%.4f%s",safe_divide(t->elapsed,p->count,1),delim);
00155 }
00156 
00157 #endif
 All Data Structures Files Functions Variables Typedefs Defines