aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/VES5.0/evel/evel-library/code/VESreporting_vAFX/afx_linkmon.c
blob: ac816b2fe20449b42adb4a79d8e339fa200bb1b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
 /*****************************************************************************//***
 * Copyright(c) <2017>, AT&T Intellectual Property.  All other rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:  This product includes
 *    software developed by the AT&T.
 * 4. Neither the name of AT&T nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>

#include "evel.h"
#include "afx_ves_reporter.h"


/**************************************************************************//**
 * Thread function to Monitor Ethernet links
 * Checks status of afx inout and afx output service every service monitor
 * interval. It reports fault event at startup and whenever status changes
 *
 * @param threadarg  Thread arguments for startup message
 *
 * @retvalue  int   Success 1 service running 0 service inactive
 *****************************************************************************/
void *LinkMonitorAfxThread(void *threadarg)
{
   int taskid, sum;
   char *hello_msg;
   struct thread_data *my_data;
    FILE *fp;
    char const* const fileName = AFX_INTERFACE_FILE;
    char line[256];
    char cmd[256];
    char intf[256];
    char descr[256];
    char linkdata[256];
    int linkcount = 0;
    char *search = ":";
    char *token;
    char *pch;
    time_t filetime = 0;
    int newrun = 1;
    int n_spaces = 0, i;
    char ** res  = NULL;
    char *pos;
    int tlinkstat;
    LINKSTAT intfstats[MAX_INTERFACES];
    char vesevid[128];
    int status = 1; /** VFS Status is Active **/


   sleep(1);
   my_data = (struct thread_data *) threadarg;
   taskid = my_data->thread_id;
   sum = my_data->sum;
   hello_msg = my_data->message;
   EVEL_INFO("AFX: Linkmon Thread %d: %s  Sum=%d\n", taskid, hello_msg, sum);

while(1)
{
    /*Check if file is modified recently */
    if( file_is_modified(fileName, &filetime) )  newrun = 1;

    FILE* file = fopen(fileName, "r"); /* should check the result */
    if( file == NULL ){
      EVEL_ERROR("AFX: Linkmon Interface config file unavailable\n");
      pthread_exit(NULL);
    }

    while ( file != NULL && fgets(line, sizeof(line), file)) {
        /* note that fgets don't strip the terminating \n, checking its
           presence would allow to handle lines longer that sizeof(line) */
        sscanf(line,"%s %s",intf,descr);
	//remove_spaces(line);
        if ((pos=strchr(line, '\n')) != NULL)
           *pos = '\0';
        //printf("%s", line);
        sprintf(cmd,"/sbin/ethtool %s\n", intf);
        //printf("%s", cmd);
        if( newrun ){
                strcpy(intfstats[linkcount].linkname,intf);
                strcpy(intfstats[linkcount].linkdescr,line);
        }

  /* Open the ethtool interface command for reading. */
  fp = popen(cmd, "r");
  if (fp == NULL) {
    EVEL_DEBUG("AFX Linkmon: Failed to run command\n" );
    //pthread_exit(NULL);
    sleep(LINK_MONITOR_INTERVAL);
    continue;
  }

  while( !feof(fp) ) {
  /* Read the output a line at a time - output it. */
  while (fgets(linkdata, sizeof(linkdata)-1, fp) != NULL) {
    //EVEL_INFO("AFX: LinkMon :%s:",linkdata);
    if ((pos=strchr(linkdata, '\n')) != NULL)
           *pos = '\0';
    if( strstr(linkdata,"Supported link modes"))
    {
          pch = strstr(linkdata, search);
          while( pch != NULL &&( *pch == ':' || *pch == ' ')) pch++;
          if(pch!=NULL){
            //printf("Link mode %s\n",pch);
            strcpy(intfstats[linkcount].linkmode,pch);
          }
    }
    else if( strstr(linkdata,"Speed"))
    {
          token = strtok(linkdata, search);
          token = strtok(NULL, search);
          intfstats[linkcount].speedmbps = atoi(token);
            //printf("Link speed %d\n",intfstats[linkcount].speedmbps);
    }
    else if( strstr(linkdata,"Link detected:"))
    {
	  /* parse link data for connectivity */
          token = strtok(linkdata, search);
          token = strtok(NULL, search);
          while( *token == ' ') token++;
          if( !strcmp(token,"yes")){
            tlinkstat = 1;
          } else {
            tlinkstat = 0;
          }
          if( newrun || tlinkstat !=  intfstats[linkcount].linkstat)
          {
            EVEL_DEBUG("AFX LinkMon Status changed report fault\n");
            if( tlinkstat == 1 ){
               sprintf(vesevid,"Fault_vAfx_LinkState_%s",oam_intfaddr);
               report_fault("Fault_vAfx_Link_Status",vesevid,EVEL_SEVERITY_NORMAL,"link",intfstats[linkcount].linkdescr,"link up trap_alarm","physical or logical connection to a remote router is up","remrouter","rname",NULL, status);
            } else {
               sprintf(vesevid,"Fault_vAfx_LinkState_%s",oam_intfaddr);
               report_fault("Fault_vAfx_Link_Status",vesevid,EVEL_SEVERITY_CRITICAL,"link",intfstats[linkcount].linkdescr,"link down trap_alarm","physical or logical connection to a remote router is down","remrouter","rname",NULL, status);
            }
          }
          intfstats[linkcount].linkstat = tlinkstat;
    }
    else if( strstr(linkdata,"Cannot get link status:"))
    {
         sprintf(vesevid,"Fault_vAfx_LinkState_%s",oam_intfaddr);
         report_fault("Fault_vAfx_Link_Status",vesevid,EVEL_SEVERITY_CRITICAL,"link",intfstats[linkcount].linkname,"link down trap_alarm","physical or logical connection to a remote router is down","remrouter","rname",NULL, status);
    }
   }
  }

  /* close */
  pclose(fp);
  linkcount++;
  }

  newrun = linkcount = 0;
  if(file)fclose(file);

    sleep(LINK_MONITOR_INTERVAL);
  }

   pthread_exit(NULL);
}