summaryrefslogtreecommitdiffstats
path: root/src/zabbix_agent/logfiles.c
blob: be1bb1736650d6e95205a183e7b8b0818a8c9444 (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
/* 
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/

#include "common.h"

#include "log.h"
#include "logfiles.h"

/******************************************************************************
 *                                                                            *
 * Function: process_log                                                      *
 *                                                                            *
 * Purpose: Get message from logfile                                          *
 *                                                                            *
 * Parameters: filename - logfile name                                        *
 *             lastlogsize - offset for message                               *
 *             value - pointer for logged message                             *
 *                                                                            *
 * Return value: returns SUCCEED on succesfull reading,                       *
 *               FAIL on other cases                                          *
 *                                                                            *
 * Author: Eugene Grigorjev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *    This function allocate memory for 'value', because use zbx_free.        *
 *    Return SUCCEED and NULL value if end of file received.                  *
 *                                                                            *
 *                                                                            *
 ******************************************************************************/
int   process_log(
	char *filename,
	long *lastlogsize,
	char **value
	)
{
	FILE	*f = NULL;
	struct stat	buf;
	int	ret = FAIL;

	assert(filename);
	assert(lastlogsize);
	assert(value);

	zabbix_log( LOG_LEVEL_DEBUG, "In process log (%s,%li)",
		filename,
		*lastlogsize);

	/* Handling of file shrinking */
	if( 0 != stat(filename,&buf) )
	{
		zabbix_log( LOG_LEVEL_WARNING, "Cannot open [%s] [%s]", filename, strerror(errno));
	}
	else
	{
		if(buf.st_size<*lastlogsize)
		{
			*lastlogsize=0;
		}

		if(NULL == (f = fopen(filename,"r") ))
		{
			zabbix_log( LOG_LEVEL_WARNING, "Cannot open [%s] [%s]", filename, strerror(errno));
		}
		else
		{
			if(-1 == fseek(f,*lastlogsize,SEEK_SET))
			{
				zabbix_log( LOG_LEVEL_WARNING, "Cannot set postition to [%li] for [%s] [%s]", *lastlogsize, filename, strerror(errno));
			}
			else
			{
				*value = zbx_malloc(*value, MAX_BUF_LEN);
				memset(*value, 0, MAX_BUF_LEN);

				if(NULL == fgets(*value, MAX_BUF_LEN-1, f))
				{
					/* EOF */
					zbx_free(*value);
				}
				else
				{
					*lastlogsize = ftell(f);
					zbx_rtrim(*value, "\r\n");
                		}
				ret = SUCCEED;
			}
			zbx_fclose(f);
		}
	}

	return ret;
}