~ubuntu-branches/ubuntu/trusty/sec/trusty-proposed

« back to all changes in this revision

Viewing changes to itostream.c

  • Committer: Bazaar Package Importer
  • Author(s): Jaakko Niemi
  • Date: 2006-10-28 22:32:35 UTC
  • mfrom: (1.2.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061028223235-eda4i78p8k992kd6
Tags: 2.4.0-1
* New upstream release
* update standards version, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  This program reads events from ITO server or agent event stream and 
3
 
  writes them to standard output. Its main task is to be a link between
4
 
  ITO and external message processing application (e.g. correlation engine).
5
 
  
6
 
  This program takes 2 commandline parameters:
7
 
  
8
 
  MSI interface - name used internally by ITO to denote the interface that
9
 
                  is used for passing messages to this program. You can
10
 
                  pick up arbitrary name, provided that it meets naming
11
 
                  convention used by ITO (see ITO Application Integration 
12
 
                  Guide for more information). Generally, using up to 12 
13
 
                  a-z characters as a name should be safe.
14
 
  reopen timeout - if there has been no new data for a given number of 
15
 
                   seconds, reopen the interface. Using 0 as a value
16
 
                   disables reopening.
17
 
 
18
 
 
19
 
  Compiling on ITO server (tested on HPUX running ITO5.3):
20
 
  gcc -o itostream itostream.c -L/opt/OV/lib -lopcsv -lnsp
21
 
  
22
 
  Compiling on ITO agent:
23
 
  gcc -o itostream itostream.c -DAGENT -L/opt/OV/lib -lopc -lnsp
24
 
 
25
 
  Since ITO agent installation is sometimes broken, you might not have libopc 
26
 
  in /opt/OV/lib (which normally is just a symbolic link to libopc_r or some 
27
 
  other library in the same directory). In that case you could just try:
28
 
  gcc -o itostream itostream.c -DAGENT -L/opt/OV/lib -lopc_r -lnsp
29
 
 
30
 
  Compiled program needs root-privileges for running. 
31
 
  If ITO shared libraries are not found when starting the program, recompile
32
 
  with additional options -Xlinker -rpath /opt/OV/lib 
33
 
*/
34
 
 
35
 
#include <stdio.h>                      /* stdio stuff */
36
 
#include <stdlib.h>                     /* free(), atoi() */
37
 
#include <unistd.h>                     /* sleep() */
38
 
 
39
 
#ifdef AGENT
40
 
#include "/opt/OV/include/opcapi.h"     /* ITO agent-side stuff */
41
 
#else
42
 
#include "/opt/OV/include/opcsvapi.h"   /* ITO server-side stuff */
43
 
#endif
44
 
 
45
 
#define ERRORMSGSIZE 1024
46
 
#define SLEEPEMPTY 1    /* sleep on empty input before new read attempt */
47
 
#define SLEEPOPEN 3     /* sleep between closing and reopening the input */
48
 
 
49
 
void error_msg(int code, char *text)
50
 
{
51
 
  int size;
52
 
  char *ptr;
53
 
 
54
 
  opcdata_get_error_msg(code, &ptr, &size);
55
 
  strncpy(text, ptr, size);
56
 
  text[size] = 0;
57
 
  free(ptr);
58
 
}
59
 
 
60
 
 
61
 
 
62
 
int open_if(char *ifname)
63
 
{
64
 
  int ret;
65
 
  char errortext[ERRORMSGSIZE];
66
 
  int interface;
67
 
 
68
 
 
69
 
#ifdef AGENT
70
 
  ret = opcif_open(OPCAGTIF_EXTMSGPROC_READWRITE, ifname, OPCIF_SV_RUNNING | 
71
 
    OPCIF_READ_NOWAIT | OPCIF_IGNORE_MSI_ALREADY_EXISTS, 0, &interface);
72
 
#else
73
 
  ret = opcif_open(OPCSVIF_EXTMSGPROC_READWRITE, ifname, OPCIF_SV_RUNNING | 
74
 
    OPCIF_READ_NOWAIT | OPCIF_IGNORE_MSI_ALREADY_EXISTS, 0, &interface);
75
 
#endif
76
 
 
77
 
  if (ret != OPC_ERR_OK) {
78
 
 
79
 
    error_msg(ret, errortext);
80
 
 
81
 
    fprintf(stderr, 
82
 
      "Error opening MSI interface \"%s\": %s\n", ifname, errortext);
83
 
 
84
 
    exit(1);
85
 
  }
86
 
 
87
 
  ret = opcif_register(interface, 0, 0);
88
 
 
89
 
  if (ret != OPC_ERR_OK) {
90
 
 
91
 
    error_msg(ret, errortext);
92
 
 
93
 
    fprintf(stderr, 
94
 
      "Error registering condition with MSI interface \"%s\": %s\n", 
95
 
        ifname, errortext);
96
 
 
97
 
    opcif_close(interface);
98
 
 
99
 
    exit(1);
100
 
  }
101
 
 
102
 
  return interface;
103
 
}
104
 
 
105
 
 
106
 
 
107
 
void find_sev(int severity, char *text)
108
 
{
109
 
 
110
 
  switch(severity) {
111
 
 
112
 
    case OPC_SEV_UNCHANGED:
113
 
      strcpy(text, "unchanged");
114
 
      break;
115
 
 
116
 
    case OPC_SEV_UNKNOWN:
117
 
      strcpy(text, "unknown");
118
 
      break;
119
 
 
120
 
    case OPC_SEV_NORMAL:
121
 
      strcpy(text, "normal");
122
 
      break;
123
 
 
124
 
    case OPC_SEV_WARNING:
125
 
      strcpy(text, "warning");
126
 
      break;
127
 
 
128
 
    case OPC_SEV_CRITICAL:
129
 
      strcpy(text, "critical");
130
 
      break;
131
 
 
132
 
    case OPC_SEV_MINOR:
133
 
      strcpy(text, "minor");
134
 
      break;
135
 
 
136
 
    case OPC_SEV_MAJOR:
137
 
      strcpy(text, "major");
138
 
      break;
139
 
 
140
 
  }
141
 
 
142
 
}
143
 
 
144
 
 
145
 
 
146
 
int main(int argc, char **argv)
147
 
{
148
 
  int interface;
149
 
  int reopen, sleepcounter;
150
 
  int ret;
151
 
  char errortext[ERRORMSGSIZE];
152
 
  opcdata msg;
153
 
  long time;
154
 
  long severity;
155
 
  char sevtext[32];
156
 
  char *id, *app, *obj, *node;
157
 
  char *msg_group, *msg_text;
158
 
 
159
 
 
160
 
  if (argc < 3) {
161
 
 
162
 
    fprintf(stderr, 
163
 
      "Usage: %s <MSI interface name> <reopen timeout>\n", argv[0]);
164
 
    exit(1);
165
 
  }
166
 
 
167
 
  /* set stdout buffering to line mode
168
 
     (needed if stdout was redirected to a file or to a pipe) */
169
 
  setvbuf(stdout, 0, _IOLBF, 0); 
170
 
 
171
 
  interface = open_if(argv[1]);
172
 
  reopen = atoi(argv[2]);
173
 
 
174
 
  opcdata_create(OPCDTYPE_EMPTY, &msg);
175
 
 
176
 
  sleepcounter = 0;
177
 
 
178
 
  for (;;) {
179
 
 
180
 
    ret = opcif_read(interface, msg);
181
 
 
182
 
    switch (ret) {
183
 
 
184
 
      case OPC_ERR_OK:
185
 
 
186
 
        sleepcounter = 0;
187
 
 
188
 
        id = opcdata_get_str(msg, OPCDATA_MSGID);
189
 
        time = opcdata_get_long(msg, OPCDATA_CREATION_TIME);
190
 
        severity = opcdata_get_long(msg, OPCDATA_SEVERITY);
191
 
        node = opcdata_get_str(msg, OPCDATA_NODENAME);
192
 
        app = opcdata_get_str(msg, OPCDATA_APPLICATION);
193
 
        obj = opcdata_get_str(msg, OPCDATA_OBJECT);
194
 
        msg_group = opcdata_get_str(msg, OPCDATA_GROUP);
195
 
        msg_text = opcdata_get_str(msg, OPCDATA_MSGTEXT);
196
 
 
197
 
        find_sev(severity, sevtext);
198
 
 
199
 
        printf("id=%s time=%ld sev=%s node=%s app=%s obj=%s msg_grp=%s msg_text=%s\n",
200
 
          id, time, sevtext, node, app, obj, msg_group, msg_text);
201
 
 
202
 
        break;
203
 
 
204
 
      case OPC_ERR_NO_DATA:
205
 
 
206
 
        sleep(SLEEPEMPTY);
207
 
        sleepcounter += SLEEPEMPTY;
208
 
 
209
 
        if (reopen  &&  sleepcounter >= reopen) {
210
 
 
211
 
          fprintf(stderr, "Reopening MSI interface \"%s\"\n", argv[1]);
212
 
          sleepcounter = 0;
213
 
          opcif_close(interface);
214
 
          sleep(SLEEPOPEN);
215
 
          interface = open_if(argv[1]);
216
 
        }
217
 
 
218
 
        break;
219
 
 
220
 
      default:
221
 
 
222
 
        error_msg(ret, errortext);
223
 
 
224
 
        fprintf(stderr, "Error reading from MSI interface \"%s\": %s\n",
225
 
          argv[1], errortext);
226
 
 
227
 
        opcdata_free(&msg);
228
 
        opcif_close(interface);
229
 
 
230
 
        exit(1);
231
 
 
232
 
    }
233
 
 
234
 
  } 
235
 
 
236
 
}
237