post
poster: Thetawaves
description: nagios status.dat
language: C
[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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// WARNING: This code segfaults and otherwise does not work as expected
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_nagios.h"
#include <stdio.h>
#include <string.h>

static function_entry nagios_functions[] = {
    PHP_FE(nagios_get_status, NULL)
    {NULL, NULL, NULL}
};

zend_module_entry nagios_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_NAGIOS_EXTNAME,
    nagios_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
#if ZEND_MODULE_API_NO >= 20010901
    PHP_NAGIOS_VERSION,
#endif
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_NAGIOS
ZEND_GET_MODULE(nagios)
#endif

void trim(char *s, const int len)
{
    int end = len - 1;
    int start = 0;
    int i = 0;

    while ((start < len) && (s[start] <= ' '))
    {
        start++;
    }

    while ((start < end) && (s[end] <= ' '))
    {
        end--;
    }

    if (start > end)
    {
        memset(s, '\0', len);
        return;
    }

    for (i = 0; (i + start) <= end; i++)
    {
        s[i] = s[start + i];
    }
    memset((s + i), '\0', len - i);
}

int is_equal(const char *str1, const char *str2)
{
    return strcmp(str1, str2) == 0;
}

int in_array(char *s, char**array, int arraysize)
{
    int i = 0;
    for (i = 0; i<arraysize; i++)
        if (strcmp(s, array[i]) == 0)
            return 1;

    return 0;

}

PHP_FUNCTION(nagios_get_status)
{
    char *hostkeys[64] = {"host_name", "has_been_checked", "check_execution_time", "check_latency",
                "check_type", "current_state", "current_attempt", "state_type",
                "last_state_change", "last_time_up", "last_time_down", "last_time_unreachable",
                "last_notification", "next_notification", "no_more_notifications",
                "current_notification_number", "notifications_enabled", "problem_has_been_acknowledged",
                "acknowledgement_type", "active_checks_enabled", "passive_checks_enabled", "last_update"};
    int hostkeySize = 22;

    char *servicekeys[64] = {"host_name", "service_description", "has_been_checked", "check_execution_time", 
                "check_latency", "current_state", "state_type", "last_state_change", "last_time_ok", 
                "last_time_warning", "last_time_unknown", "last_time_critical", "plugin_output", 
                "last_check", "notifications_enabled", "active_checks_enabled", "passive_checks_enabled", 
                "problem_has_been_acknowledged", "acknowledgement_type", "last_update", "is_flapping"};
    int servicekeySize = 21;

    FILE *statusfile;
    
    void *bufferptr = malloc(512);
    char * lineptr = (char *) bufferptr;

    int buffer_size = 512;
    int linenumber = 0;
    int line_size = 0;

    char *filename;
    int filename_len;
    
    int inSection = 0;
    char *sectionType = malloc(buffer_size);
    int sectionTypeSize = 0;

    char *linekey = malloc(buffer_size);
    int linekeySize = 0;

    char *lineval = malloc(buffer_size);
    int linevalSize = 0;

    zval * sectionData;
    
    zval * serviceStatus;
    zval * hostStatus;

    zval * serviceDescription;
    
    zval **tmp;

    char *hostname;
    char *servdesc;
    int hostnameSize = 0;
    int servdescSize = 0;

    array_init(serviceStatus);
    array_init(hostStatus);

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
        RETURN_NULL();
    }
    
    php_printf("Hello %s ", filename);
    
    statusfile = fopen(filename, "r");
    if (statusfile == NULL)
    {
        php_printf("Error opening file.");
        free(bufferptr);
        RETURN_FALSE;
    }
    while((line_size = getline(&lineptr, &buffer_size, statusfile)) != EOF)
    {
        linenumber++;
        trim(lineptr, line_size);

        if (strlen(lineptr) == 0) continue;
        if (lineptr[0] == '#') continue;
        if (!inSection)
        {
            if (strchr(lineptr, ' ') && lineptr[strlen(lineptr) - 1] == '{')
            {
                sectionTypeSize = strchr(lineptr, ' ') - lineptr;
                strncpy(sectionType, lineptr, sectionTypeSize);
                sectionType[sectionTypeSize] = '\0';
                
                inSection = 1;

                array_init(sectionData);
                
                php_printf("%s|\n", sectionType);
            }
    
        }
        else if (inSection && lineptr[0] == '}')
        {

            if (is_equal(sectionType, "service"))
            {
                if (zend_symtable_find(HASH_OF(sectionData), "host_name", strlen("host_name"), (void **)&tmp) == SUCCESS)
                {

                    if (Z_TYPE_PP(tmp) == IS_STRING)
                    {
                        hostname = Z_STRVAL_PP(tmp);
                        hostnameSize = Z_STRLEN_PP(tmp);
                    }
                    
                    if (zend_symtable_find(HASH_OF(sectionData), "service_description", strlen("service_description"), (void **)&tmp) == SUCCESS)
                    {
                        
                        if (Z_TYPE_PP(tmp) == IS_STRING)
                        {
                            servdesc = Z_STRVAL_PP(tmp);
                            servdescSize = Z_STRLEN_PP(tmp);
                        }

                    }

                }
                if (hostnameSize != 0 && servdescSize != 0)
                {
                    if (zend_symtable_find(HASH_OF(serviceStatus), hostname, hostnameSize, (void **)&tmp) == SUCCESS)
                    {
                        add_assoc_zval_ex(tmp, servdesc, servdescSize, sectionData);
                    }
                    else
                    {
                        array_init(serviceDescription);
                        add_assoc_zval_ex(serviceDescription, servdesc, servdescSize, sectionData);
                        add_assoc_zval_ex(serviceStatus, hostname, hostnameSize, serviceDescription);
                        
                    }

                }
            }
            if (is_equal(sectionType, "host"))
            {
                if (zend_symtable_find(HASH_OF(sectionData), "host_name", strlen("host_name"), (void **)&tmp) == SUCCESS)
                {
                    if (Z_TYPE_PP(tmp) == IS_STRING)
                    {
                        hostname = Z_STRVAL_PP(tmp);
                        hostnameSize = Z_STRLEN_PP(tmp);
                    }
                    
                }
                if (hostnameSize != 0)
                {
                    add_assoc_zval_ex(hostStatus, hostname, hostnameSize, sectionData);
                }
            }
            
            inSection = 0;
            strncpy(sectionType, "\0\0", buffer_size);
        }
        else
        {
            linekeySize = strchr(lineptr, '=') - lineptr;
            strncpy(linekey, lineptr, linekeySize);
            linekey[linekeySize] = '\0';

            linevalSize = (lineptr + strlen(lineptr)) - strchr(lineptr, '=');
            strncpy(lineval, strchr(lineptr, '=') + 1, linevalSize);
            lineval[linevalSize] = '\0';

            if (is_equal(sectionType, "service"))
            {
                if (in_array(linekey, servicekeys, servicekeySize))
                {
                    add_assoc_string(sectionData, linekey, lineval, 1);
                }
            }
            if (is_equal(sectionType, "host"))
            {
                if (in_array(linekey, hostkeys, hostkeySize))
                {
                    add_assoc_string(sectionData, linekey, lineval, 1);
                }
            }

            // else continue on, ignore this section, don't save anything
        }
        //php_printf("%s\n", lineptr);
    }
    array_init(return_value);
    add_assoc_zval(return_value, "service", serviceStatus);
    add_assoc_zval(return_value, "host", hostStatus);

    fclose(statusfile);
    free(lineval);
    free(linekey);
    free(sectionType);
    free(bufferptr);
}