File: itostream.c

package info (click to toggle)
sec 2.3.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 392 kB
  • ctags: 135
  • sloc: perl: 5,017; ansic: 135; makefile: 4
file content (237 lines) | stat: -rw-r--r-- 5,750 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
  This program reads events from ITO server or agent event stream and 
  writes them to standard output. Its main task is to be a link between
  ITO and external message processing application (e.g. correlation engine).
  
  This program takes 2 commandline parameters:
  
  MSI interface - name used internally by ITO to denote the interface that
                  is used for passing messages to this program. You can
                  pick up arbitrary name, provided that it meets naming
                  convention used by ITO (see ITO Application Integration 
                  Guide for more information). Generally, using up to 12 
                  a-z characters as a name should be safe.
  reopen timeout - if there has been no new data for a given number of 
                   seconds, reopen the interface. Using 0 as a value
                   disables reopening.


  Compiling on ITO server (tested on HPUX running ITO5.3):
  gcc -o itostream itostream.c -L/opt/OV/lib -lopcsv -lnsp
  
  Compiling on ITO agent:
  gcc -o itostream itostream.c -DAGENT -L/opt/OV/lib -lopc -lnsp

  Since ITO agent installation is sometimes broken, you might not have libopc 
  in /opt/OV/lib (which normally is just a symbolic link to libopc_r or some 
  other library in the same directory). In that case you could just try:
  gcc -o itostream itostream.c -DAGENT -L/opt/OV/lib -lopc_r -lnsp

  Compiled program needs root-privileges for running. 
  If ITO shared libraries are not found when starting the program, recompile
  with additional options -Xlinker -rpath /opt/OV/lib 
*/

#include <stdio.h>                      /* stdio stuff */
#include <stdlib.h>                     /* free(), atoi() */
#include <unistd.h>                     /* sleep() */

#ifdef AGENT
#include "/opt/OV/include/opcapi.h"     /* ITO agent-side stuff */
#else
#include "/opt/OV/include/opcsvapi.h"   /* ITO server-side stuff */
#endif

#define ERRORMSGSIZE 1024
#define SLEEPEMPTY 1	/* sleep on empty input before new read attempt */
#define SLEEPOPEN 3	/* sleep between closing and reopening the input */

void error_msg(int code, char *text)
{
  int size;
  char *ptr;

  opcdata_get_error_msg(code, &ptr, &size);
  strncpy(text, ptr, size);
  text[size] = 0;
  free(ptr);
}



int open_if(char *ifname)
{
  int ret;
  char errortext[ERRORMSGSIZE];
  int interface;


#ifdef AGENT
  ret = opcif_open(OPCAGTIF_EXTMSGPROC_READWRITE, ifname, OPCIF_SV_RUNNING | 
    OPCIF_READ_NOWAIT | OPCIF_IGNORE_MSI_ALREADY_EXISTS, 0, &interface);
#else
  ret = opcif_open(OPCSVIF_EXTMSGPROC_READWRITE, ifname, OPCIF_SV_RUNNING | 
    OPCIF_READ_NOWAIT | OPCIF_IGNORE_MSI_ALREADY_EXISTS, 0, &interface);
#endif

  if (ret != OPC_ERR_OK) {

    error_msg(ret, errortext);

    fprintf(stderr, 
      "Error opening MSI interface \"%s\": %s\n", ifname, errortext);

    exit(1);
  }

  ret = opcif_register(interface, 0, 0);

  if (ret != OPC_ERR_OK) {

    error_msg(ret, errortext);

    fprintf(stderr, 
      "Error registering condition with MSI interface \"%s\": %s\n", 
        ifname, errortext);

    opcif_close(interface);

    exit(1);
  }

  return interface;
}



void find_sev(int severity, char *text)
{

  switch(severity) {

    case OPC_SEV_UNCHANGED:
      strcpy(text, "unchanged");
      break;

    case OPC_SEV_UNKNOWN:
      strcpy(text, "unknown");
      break;

    case OPC_SEV_NORMAL:
      strcpy(text, "normal");
      break;

    case OPC_SEV_WARNING:
      strcpy(text, "warning");
      break;

    case OPC_SEV_CRITICAL:
      strcpy(text, "critical");
      break;

    case OPC_SEV_MINOR:
      strcpy(text, "minor");
      break;

    case OPC_SEV_MAJOR:
      strcpy(text, "major");
      break;

  }

}



int main(int argc, char **argv)
{
  int interface;
  int reopen, sleepcounter;
  int ret;
  char errortext[ERRORMSGSIZE];
  opcdata msg;
  long time;
  long severity;
  char sevtext[32];
  char *id, *app, *obj, *node;
  char *msg_group, *msg_text;


  if (argc < 3) {

    fprintf(stderr, 
      "Usage: %s <MSI interface name> <reopen timeout>\n", argv[0]);
    exit(1);
  }

  /* set stdout buffering to line mode
     (needed if stdout was redirected to a file or to a pipe) */
  setvbuf(stdout, 0, _IOLBF, 0); 

  interface = open_if(argv[1]);
  reopen = atoi(argv[2]);

  opcdata_create(OPCDTYPE_EMPTY, &msg);

  sleepcounter = 0;

  for (;;) {

    ret = opcif_read(interface, msg);

    switch (ret) {

      case OPC_ERR_OK:

        sleepcounter = 0;

        id = opcdata_get_str(msg, OPCDATA_MSGID);
        time = opcdata_get_long(msg, OPCDATA_CREATION_TIME);
        severity = opcdata_get_long(msg, OPCDATA_SEVERITY);
        node = opcdata_get_str(msg, OPCDATA_NODENAME);
        app = opcdata_get_str(msg, OPCDATA_APPLICATION);
        obj = opcdata_get_str(msg, OPCDATA_OBJECT);
        msg_group = opcdata_get_str(msg, OPCDATA_GROUP);
        msg_text = opcdata_get_str(msg, OPCDATA_MSGTEXT);

	find_sev(severity, sevtext);

        printf("id=%s time=%ld sev=%s node=%s app=%s obj=%s msg_grp=%s msg_text=%s\n",
          id, time, sevtext, node, app, obj, msg_group, msg_text);

        break;

      case OPC_ERR_NO_DATA:

        sleep(SLEEPEMPTY);
        sleepcounter += SLEEPEMPTY;

        if (reopen  &&  sleepcounter >= reopen) {

          fprintf(stderr, "Reopening MSI interface \"%s\"\n", argv[1]);
          sleepcounter = 0;
          opcif_close(interface);
	  sleep(SLEEPOPEN);
          interface = open_if(argv[1]);
        }

        break;

      default:

        error_msg(ret, errortext);

        fprintf(stderr, "Error reading from MSI interface \"%s\": %s\n",
          argv[1], errortext);

        opcdata_free(&msg);
        opcif_close(interface);

        exit(1);

    }

  } 

}