e8ff5d4285666b0c53e13eb1c9d81f803c8dfeed
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / VESreporting_vAFX / afx_svc.c
1  /*****************************************************************************//***
2  * Copyright(c) <2017>, AT&T Intellectual Property.  All other rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:  This product includes
14  *    software developed by the AT&T.
15  * 4. Neither the name of AT&T nor the names of its contributors may be used to
16  *    endorse or promote products derived from this software without specific
17  *    prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *****************************************************************************/
30
31 #include <pthread.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41
42 #include "evel.h"
43 #include "afx_ves_reporter.h"
44
45 /**************************************************************************//**
46  * Monitor AFX services for current status
47  * returns success status 1 or failure 0.
48  *
49  * @param cmd       Pointer to Shell cmd to monitor status.
50  * @param dtls      Pointer to details of service
51  *                  Returns the start time of service
52  *                  
53  * @retvalue  int   Success 1 service running 0 service inactive 
54  *****************************************************************************/
55 int monitor_afx_service( char *cmd, char *detls, char *svc )
56 {
57     FILE *fp;
58     char line[256];
59     int n_spaces = 0, i;
60     char ** res  = NULL;
61     char *p;
62     int ret = 0;
63     int lnno = 0;
64
65
66   fp = popen(cmd,"r");
67   if (fp == NULL) {
68     printf("Failed to run command\n" );
69     return 0;
70   }
71
72   /* Read the output a line at a time - output it. */
73   while (fgets(line, sizeof(line)-1, fp) != NULL) {
74     //printf("%s",line);
75     lnno++;
76
77 n_spaces = 0;
78 p    = strtok (line, " ");
79 /* split string and append tokens to 'res' */
80
81 while (p) {
82   res = realloc (res, sizeof (char*) * ++n_spaces);
83
84   if (res == NULL)
85     exit (-1); /* memory allocation failed */
86
87   res[n_spaces-1] = p;
88
89   p = strtok (NULL, " ");
90 }
91
92 /* realloc one extra element for the last NULL */
93 res = realloc (res, sizeof (char*) * (n_spaces+1));
94 res[n_spaces] = 0;
95
96 if( n_spaces > 2 && lnno == 1 )
97 {
98   EVEL_DEBUG("AFX: Service %s\n",res[3]);
99   sprintf(svc,"%s",res[3]);
100   svc[strlen(svc)-1] = 0;
101 }
102
103 /* print the result */
104 if( n_spaces > 2 && !strcmp(res[0],"Active:") )
105 {
106   //for (i = 0; i < (n_spaces+1); ++i)
107   //{
108   // printf ("res[%d] = %s\n", i, res[i]);
109   //}
110   if(!strncmp(res[1],"activ",5)){
111      EVEL_DEBUG("AFX: Service Active\n");
112      ret |= 1;
113   } else {
114      EVEL_DEBUG("AFX: Service Inactive\n");
115      ret &= 0;
116   }
117 }
118
119   }
120
121 /* free the memory allocated */
122 if(res != NULL) free (res);
123
124   /* close */
125   pclose(fp);
126
127 return ret;
128
129 }
130
131 /**************************************************************************//**
132  * Thread function to Monitor AFX services
133  * Checks status of afx inout and afx output service every service monitor
134  * interval. It reports fault event at startup and whenever status changes
135  *
136  * @param threadarg  Thread arguments for startup message
137  *
138  * @retvalue  int   Success 1 service running 0 service inactive
139  *****************************************************************************/
140 void *ServiceMonitorAfxThread(void *threadarg)
141 {
142    int taskid, sum;
143    char *hello_msg;
144    struct thread_data *my_data;
145    char cmd[256];
146    char details[256];
147    char *afxin = "afx@in*";
148    char *afxout = "afx@out*";
149    int instate = -1;
150    int outstate = -1;
151    int tmpstate;
152    char evid[128];
153    char svc[128];
154    int is_critical = 0;
155
156    sleep(1);
157    my_data = (struct thread_data *) threadarg;
158    taskid = my_data->thread_id;
159    sum = my_data->sum;
160    hello_msg = my_data->message;
161    EVEL_INFO("AFX: Service Monitor Thread %d: %s  Sum=%d\n", taskid, hello_msg, sum);
162
163 while(1)
164 {
165     /* invokes afx service monitor function and collects return value */
166     sprintf(cmd,"/bin/systemctl status %s ",afxin);
167     tmpstate = monitor_afx_service( cmd, details,svc );
168     if( tmpstate != instate ){
169        printf("New afxin Service report sent %d\n",tmpstate);
170        instate = tmpstate;
171        if( instate == 1){
172          sprintf(evid,"Fault_vAfx_SvcInput_%s",oam_intfaddr);
173          if (is_critical == 1){
174            is_critical = 0;
175            sprintf(evid,"Fault_vAfx_SvcOutput_SvcInput_%s",oam_intfaddr);
176            report_fault("Fault_vAfx_Svc_input_Output",evid,
177                       EVEL_SEVERITY_NORMAL, "routing", "AFXInput and AFXOutput", 
178                       "AFX services input and output down alarm", 
179                       details, NULL, NULL, NULL, instate);
180          }
181          sprintf(evid,"Fault_vAfx_SvcInput_%s",oam_intfaddr);
182          report_fault("Fault_vAfx_SvcInput",evid,EVEL_SEVERITY_NORMAL, "routing", "AFXInput", "AFX services input up alarm", details, NULL, NULL, NULL, instate);
183        }else{
184          sprintf(evid,"Fault_vAfx_SvcInput_%s",oam_intfaddr);
185          report_fault("Fault_vAfx_SvcInput",evid,EVEL_SEVERITY_MAJOR, "routing", "AFXInput", "AFX services input down alarm", details, NULL, NULL, NULL, instate);
186        }
187     }
188
189     /* invokes afx service monitor function and collects return value */
190     sprintf(cmd,"/bin/systemctl status %s ",afxout);
191     tmpstate = monitor_afx_service( cmd, details, svc );
192     if( tmpstate != outstate ){
193        printf("New afxout Service report sent %d\n",tmpstate);
194        outstate = tmpstate;
195        if( outstate == 1){
196          if (is_critical == 1){
197            is_critical = 0;
198            sprintf(evid,"Fault_vAfx_SvcOutput_SvcInput_%s",oam_intfaddr);
199            report_fault("Fault_vAfx_Svc_input_Output",evid,
200                       EVEL_SEVERITY_NORMAL, "routing", "AFXInput and AFXOutput", 
201                       "AFX services input and output down alarm", 
202                       details, NULL, NULL, NULL, outstate);
203            }
204          sprintf(evid,"Fault_vAfx_SvcOutput_%s",oam_intfaddr);
205          report_fault("Fault_vAfx_SvcOutput",evid,EVEL_SEVERITY_NORMAL, "routing", "AFXOutput", "AFX services output up alarm", details, NULL, NULL, NULL, outstate );
206        } else {
207          sprintf(evid,"Fault_vAfx_SvcOutput_%s",oam_intfaddr);
208          report_fault("Fault_vAfx_SvcOutput",evid,EVEL_SEVERITY_MAJOR, "routing", "AFXOutput", "AFX services output down alarm", details, NULL, NULL, NULL, outstate);
209        }
210     }
211     if((instate == 0) && (outstate == 0) && (is_critical == 0))
212     {
213          is_critical = 1;
214           
215          sprintf(evid,"Fault_vAfx_SvcOutput_SvcInput_%s",oam_intfaddr);
216          report_fault("Fault_vAfx_Svc_input_Output",evid,EVEL_SEVERITY_CRITICAL,
217                       "routing", "AFXInput and AFXOutput", 
218                       "AFX services input and output down alarm", details, 
219                       NULL, NULL, NULL, instate);
220     }
221
222     sleep(SERVICE_MONITOR_INTERVAL);
223 }
224
225    pthread_exit(NULL);
226 }
227
228