~kampka/ubuntu/quantal/zabbix/upstart-support

« back to all changes in this revision

Viewing changes to src/libs/zbxcommon/misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Ablassmeier
  • Date: 2009-06-28 19:11:29 UTC
  • mfrom: (19.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090628191129-5esmmf2h3tbff1fv
* New upstream release
* Merge upstream config file to template (Closes: #528426)
* Move logrotate from Depends to Suggests (Closes: #534096)
* Server should not crash anymore if postgresql transaction fails
  (Closes: #520197)
* Update fr.po (Closes: #527559)
* Update es.po (Closes: #527600)
* Update sv.po (Closes: #528571)
* Update it.po (Closes: #529157)
* Update cs.po (Closes: #529502)
* Update de.po (Closes: #532344)

Show diffs side-by-side

added added

removed removed

Lines of Context:
157
157
 
158
158
/******************************************************************************
159
159
 *                                                                            *
 
160
 * Function: get_flexible_interval                                            *
 
161
 *                                                                            *
 
162
 * Purpose: check for flexible delay value                                    *
 
163
 *                                                                            *
 
164
 * Parameters: delay_flex - [IN] separeated flexible intervals                *
 
165
 *                          [dd/d1-d2,hh:mm-hh:mm;]                           *
 
166
 *             delay_val - [OUT] delay value                                  *
 
167
 *                                                                            *
 
168
 * Return value: nextcheck value                                              *
 
169
 *                                                                            *
 
170
 * Author: Alexei Vladishev, Alexander Vladishev                              *
 
171
 *                                                                            *
 
172
 ******************************************************************************/
 
173
static int get_flexible_interval(char *delay_flex, int *delay_val, time_t now)
 
174
{
 
175
        char    *s, *c = NULL, delay_period[30];
 
176
        int     delay, ret = FAIL;
 
177
 
 
178
        if (NULL == delay_flex || '\0' == *delay_flex)
 
179
                return FAIL;
 
180
 
 
181
        for (s = delay_flex; '\0' != *s;)
 
182
        {
 
183
                if (NULL != (c = strchr(s, ';')))
 
184
                        *c = '\0';
 
185
 
 
186
                zabbix_log(LOG_LEVEL_DEBUG, "Delay period [%s]", s);
 
187
 
 
188
                if (2 == sscanf(s, "%d/%29s", &delay, delay_period))
 
189
                {
 
190
                        zabbix_log(LOG_LEVEL_DEBUG, "%d sec at %s", delay, delay_period);
 
191
 
 
192
                        if (0 != check_time_period(delay_period, now))
 
193
                        {
 
194
                                *delay_val = delay;
 
195
                                ret = SUCCEED;
 
196
                                break;
 
197
                        }
 
198
                }
 
199
                else
 
200
                        zabbix_log(LOG_LEVEL_ERR, "Delay period format is wrong [%s]", s);
 
201
 
 
202
                if (NULL != c)
 
203
                {
 
204
                        *c = ';';
 
205
                        s = c + 1;
 
206
                }
 
207
                else
 
208
                        break;
 
209
        }
 
210
 
 
211
        if (NULL != c)
 
212
                *c = ';';
 
213
 
 
214
        return ret;
 
215
}
 
216
 
 
217
/******************************************************************************
 
218
 *                                                                            *
 
219
 * Function: get_next_flexible_interval                                       *
 
220
 *                                                                            *
 
221
 * Purpose: return time of next flexible interval                             *
 
222
 *                                                                            *
 
223
 * Parameters: delay_flex - [IN] ';' separeated flexible intervals            *
 
224
 *                          [dd/d1-d2,hh:mm-hh:mm]                            *
 
225
 *             now = [IN] current time                                        *
 
226
 *                                                                            *
 
227
 * Return value: start of next interval                                       *
 
228
 *                                                                            *
 
229
 * Author: Alexei Vladishev, Alexander Vladishev                              *
 
230
 *                                                                            *
 
231
 ******************************************************************************/
 
232
static time_t   get_next_flexible_interval(char *delay_flex, time_t now)
 
233
{
 
234
        char            *s, *c = NULL;
 
235
        struct tm       *tm;
 
236
        int             day, sec, sec1, sec2, delay, d1, d2, h1, h2, m1, m2;
 
237
        time_t          next = 0;
 
238
 
 
239
        if (NULL == delay_flex || '\0' == *delay_flex)
 
240
                return FAIL;
 
241
 
 
242
        tm = localtime(&now);
 
243
        day = 0 == tm->tm_wday ? 7 : tm->tm_wday;
 
244
        sec = 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
 
245
 
 
246
        for (s = delay_flex; '\0' != *s;)
 
247
        {
 
248
                if (NULL != (c = strchr(s, ';')))
 
249
                        *c = '\0';
 
250
 
 
251
                zabbix_log(LOG_LEVEL_DEBUG, "Delay period [%s]", s);
 
252
 
 
253
                if (7 == sscanf(s, "%d/%d-%d,%d:%d-%d:%d", &delay, &d1, &d2, &h1, &m1, &h2, &m2))
 
254
                {
 
255
                        zabbix_log(LOG_LEVEL_DEBUG, "%d/%d-%d,%d:%d-%d:%d", delay, d1, d2, h1, m1, h2, m2);
 
256
 
 
257
                        sec1 = 3600 * h1 + 60 * m1;
 
258
                        sec2 = 3600 * h2 + 60 * m2;
 
259
 
 
260
                        if (day >= d1 && day <= d2 && sec >= sec1 && sec <= sec2)       /* working period */
 
261
                        {
 
262
                                if (next == 0 || next > now - sec + sec2)
 
263
                                        next = now - sec + sec2;
 
264
                                break;
 
265
                        }
 
266
 
 
267
                        if (day >= d1 && day <= d2 && sec < sec1)                       /* next period, same day */
 
268
                        {
 
269
                                if (next == 0 || next > now - sec + sec1)
 
270
                                        next = now - sec + sec1;
 
271
                        }
 
272
                        else if (day + 1 >= d1 && day + 1 <= d2 && sec < sec1)          /* next period, next  day */
 
273
                        {
 
274
                                if (next == 0 || next > now - sec + sec1)
 
275
                                        next = now - sec + 86400 + sec1;
 
276
                        }
 
277
                }
 
278
                else
 
279
                        zabbix_log(LOG_LEVEL_ERR, "Delay period format is wrong [%s]", s);
 
280
 
 
281
                if (NULL != c)
 
282
                {
 
283
                        *c = ';';
 
284
                        s = c + 1;
 
285
                }
 
286
                else
 
287
                        break;
 
288
        }
 
289
 
 
290
        if (NULL != c)
 
291
                *c = ';';
 
292
 
 
293
        return next ? next : FAIL;
 
294
}
 
295
/******************************************************************************
 
296
 *                                                                            *
160
297
 * Function: calculate_item_nextcheck                                         *
161
298
 *                                                                            *
162
299
 * Purpose: calculate nextcheck timespamp for item                            *
175
312
 ******************************************************************************/
176
313
int     calculate_item_nextcheck(zbx_uint64_t itemid, int item_type, int delay, char *delay_flex, time_t now)
177
314
{
178
 
        int     i, delay_val;
179
 
        char    *s, *c = NULL, delay_period[30];
 
315
        int     i, flex_delay2 = delay, flex_delay = delay;
 
316
        time_t  next;
180
317
 
181
318
        zabbix_log(LOG_LEVEL_DEBUG, "In calculate_item_nextcheck (" ZBX_FS_UI64 ",%d,\"%s\",%d)",
182
319
                        itemid, delay, delay_flex, now);
196
333
                return i;
197
334
        }
198
335
 
199
 
        if (NULL != delay_flex && '\0' != *delay_flex)
200
 
        {
201
 
                for (s = delay_flex; '\0' != *s;)
202
 
                {
203
 
                        if (NULL != (c = strchr(s, ';')))
204
 
                                *c = '\0';
205
 
 
206
 
                        zabbix_log(LOG_LEVEL_DEBUG, "Delay period [%s]", s);
207
 
 
208
 
                        if (2 == sscanf(s, "%d/%29s", &delay_val, delay_period))
209
 
                        {
210
 
                                zabbix_log(LOG_LEVEL_DEBUG, "%d sec at %s", delay_val, delay_period);
211
 
 
212
 
                                if (check_time_period(delay_period, now))
213
 
                                {
214
 
                                        delay = delay_val;
215
 
                                        break;
216
 
                                }
217
 
                        }
218
 
                        else
219
 
                                zabbix_log(LOG_LEVEL_ERR, "Delay period format is wrong [%s]", s);
220
 
 
221
 
                        if (NULL != c)
222
 
                        {
223
 
                                *c = ';';
224
 
                                s = c + 1;
225
 
                        }
226
 
                        else
227
 
                                break;
228
 
                }
229
 
 
230
 
                if (NULL != c)
231
 
                        *c = ';';
232
 
        }
233
 
 
234
 
        if (0 == delay)
235
 
        {
236
 
                zabbix_log(LOG_LEVEL_ERR, "Invalid item update interval [%d], using default [%d]", delay, 30);
237
 
                delay = 30;
238
 
        }
239
 
 
 
336
        get_flexible_interval(delay_flex, &flex_delay, now);
 
337
 
 
338
        if (FAIL != (next = get_next_flexible_interval(delay_flex, now)) && now + flex_delay > next)
 
339
        {
 
340
                get_flexible_interval(delay_flex, &flex_delay2, next + 1);
 
341
 
 
342
                now = next;
 
343
                flex_delay = MIN(flex_delay, flex_delay2);
 
344
        }
 
345
 
 
346
        if (0 == flex_delay)
 
347
        {
 
348
                zabbix_log(LOG_LEVEL_ERR, "Invalid item update interval [%d], using default [%d]", flex_delay, 30);
 
349
                flex_delay = 30;
 
350
        }
 
351
 
 
352
        delay = flex_delay;
240
353
        i = delay * (int)(now / (time_t)delay) + (int)(itemid % (zbx_uint64_t)delay);
241
354
 
242
355
        while (i <= now)
770
883
{
771
884
        char            *s, *c = NULL;
772
885
        int             d1, d2, h1, h2, m1, m2;
773
 
        int             day, min;
 
886
        int             day, sec;
774
887
        struct tm       *tm;
775
888
        int             ret = 0;
776
889
 
784
897
        day = tm->tm_wday;
785
898
        if(0 == day)
786
899
                day=7;
787
 
        min = 60 * tm->tm_hour + tm->tm_min;
 
900
        sec = 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
788
901
 
789
902
        zabbix_log(LOG_LEVEL_DEBUG, "%d,%d:%d", day, (int)tm->tm_hour, (int)tm->tm_min);
790
903
 
799
912
                {
800
913
                        zabbix_log(LOG_LEVEL_DEBUG, "%d-%d,%d:%d-%d:%d", d1, d2, h1, m1, h2, m2);
801
914
 
802
 
                        if (day >= d1 && day <= d2 && min >= 60 * h1 + m1 && min <= 60 * h2 + m2)
 
915
                        if (day >= d1 && day <= d2 && sec >= 3600 * h1 + 60 * m1 && sec <= 3600 * h2 + 60 * m2)
803
916
                        {
804
917
                                ret = 1;
805
918
                                break;