vpp metrics upload via gmond plugin
[vpp.git] / gmod / gmod / mod_vpp.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <gm_metric.h>
17
18 #include <stdlib.h>
19 #include <strings.h>
20 #include <time.h>
21 #include <vppinfra/clib.h>
22 #include <vppinfra/vec.h>
23 #include <vppinfra/hash.h>
24 #include <svmdb.h>
25 #include <errno.h>
26
27 mmodule vpp_module;
28 static svmdb_client_t *svmdb_client;
29
30 static int vpp_metric_init (apr_pool_t *p)
31 {
32     const char* str_params = vpp_module.module_params;
33     apr_array_header_t *list_params = vpp_module.module_params_list;
34     mmparam *params;
35     char *chroot_path = 0;
36     int i;
37
38     if (str_params) {
39         debug_msg("[mod_vpp]Received string params: %s", str_params);
40     }
41     /* Multiple name/value pair parameters. */
42     if (list_params) {
43         debug_msg("[mod_vpp]Received following params list: ");
44         params = (mmparam*) list_params->elts;
45         for(i=0; i< list_params->nelts; i++) {
46             debug_msg("\tParam: %s = %s", params[i].name, params[i].value);
47         }
48     }
49
50     svmdb_client = svmdb_map_chroot (chroot_path);
51
52
53     /* Initialize the metadata storage for each of the metrics and then
54      *  store one or more key/value pairs.  The define MGROUPS defines
55      *  the key for the grouping attribute. */
56     for (i = 0; vpp_module.metrics_info[i].name != NULL; i++) {
57         MMETRIC_INIT_METADATA(&(vpp_module.metrics_info[i]),p);
58         MMETRIC_ADD_METADATA(&(vpp_module.metrics_info[i]),MGROUP,"VPP");
59     }
60
61     return 0;
62 }
63
64 static void vpp_metric_cleanup (void)
65 {
66     svmdb_unmap (svmdb_client);
67 }
68
69 static g_val_t vpp_metric_handler (int metric_index)
70 {
71     g_val_t val;
72     pid_t *vpp_pidp;
73     f64 *vector_ratep, *vpp_rx_ratep;
74
75     switch (metric_index) {
76     case 0:
77         vector_ratep = svmdb_local_get_vec_variable 
78             (svmdb_client, "vlib_vector_rate", sizeof (*vector_ratep));
79         if (vector_ratep) {
80             val.d = *vector_ratep;
81             vec_free (vector_ratep);
82         }
83         else
84             val.d = 0.0;
85         break;
86     case 1:
87         vpp_pidp = svmdb_local_get_vec_variable 
88             (svmdb_client, 
89              "vpp_pid", sizeof (*vpp_pidp));
90         if (vpp_pidp && *vpp_pidp) {
91             if (kill(*vpp_pidp, 0) == 0 || errno != ESRCH) {
92                 val.d = 1.0;
93             } else {
94                 val.d = 0.0;
95                 }
96             vec_free (vpp_pidp);
97         } else 
98             val.d = 0;
99         break;
100
101     case 2:
102         vpp_rx_ratep = svmdb_local_get_vec_variable 
103             (svmdb_client, "vnet_input_rate", sizeof (*vector_ratep));
104         if (vpp_rx_ratep) {
105             val.d = *vpp_rx_ratep;
106             vec_free (vpp_rx_ratep);
107         } else
108             val.d = 0.0;
109         break;
110
111     default:
112         val.d = 0.0; 
113     }
114
115     return val;
116 }
117
118 static Ganglia_25metric vpp_metric_info[] = 
119 {
120     {0, "Vector_Rate", 100, GANGLIA_VALUE_DOUBLE, "Packets/Frame", 
121      "both", "%.1f", 
122      UDP_HEADER_SIZE+8, "VPP Vector Rate"},
123     {0, "VPP_State", 100, GANGLIA_VALUE_DOUBLE, "Run=1", "both", "%.0f", 
124      UDP_HEADER_SIZE+8, "VPP State"},
125     {0, "Input_Rate", 100, GANGLIA_VALUE_DOUBLE, "5 sec RX rate", 
126      "both", "%.1f", 
127      UDP_HEADER_SIZE+8, "VPP Aggregate RX Rate"},
128     {0, NULL}
129 };
130
131 mmodule vpp_module =
132 {
133     STD_MMODULE_STUFF,
134     vpp_metric_init,
135     vpp_metric_cleanup,
136     vpp_metric_info,
137     vpp_metric_handler,
138 };