src/utils_logtail.[ch]: Implement a module that parses logfiles using the `utils_tail...
[collectd.git] / src / utils_tail.c
1 /**
2  * collectd - src/utils_tail.c
3  * Copyright (C) 2007-2008  C-Ware, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Luke Heberling <lukeh at c-ware.com>
20  *
21  * Description:
22  *   Encapsulates useful code for plugins which must watch for appends to
23  *   the end of a file.
24  **/
25
26 #include "collectd.h"
27 #include "common.h"
28 #include "utils_tail.h"
29
30 struct cu_tail_s
31 {
32         char  *file;
33         FILE  *fd;
34         struct stat stat;
35 };
36
37 cu_tail_t *cu_tail_create (const char *file)
38 {
39         cu_tail_t *obj;
40
41         obj = (cu_tail_t *) malloc (sizeof (cu_tail_t));
42         if (obj == NULL)
43                 return (NULL);
44         memset (obj, '\0', sizeof (cu_tail_t));
45
46         obj->file = strdup (file);
47         if (obj->file == NULL)
48         {
49                 free (obj);
50                 return (NULL);
51         }
52
53         obj->fd = NULL;
54
55         return (obj);
56 } /* cu_tail_t *cu_tail_create */
57
58 int cu_tail_destroy (cu_tail_t *obj)
59 {
60         if (obj->fd != NULL)
61                 fclose (obj->fd);
62         free (obj->file);
63         free (obj);
64
65         return (0);
66 } /* int cu_tail_destroy */
67
68 int cu_tail_readline (cu_tail_t *obj, char *buf, int buflen)
69 {
70         struct stat stat_now;
71         int status;
72
73         if (buflen < 1)
74                 return (-1);
75         
76         if (stat (obj->file, &stat_now) != 0)
77         {
78                 char errbuf[1024];
79                 ERROR ("cu_tail_readline: stat (%s) failed: %s",
80                                 obj->file,
81                                 sstrerror (errno, errbuf, sizeof (errbuf)));
82                 return (-1);
83         }
84
85         if ((stat_now.st_dev != obj->stat.st_dev) ||
86                 (stat_now.st_ino != obj->stat.st_ino))
87         {
88                 /*
89                  * If the file was replaced open the new file and close the
90                  * old filehandle
91                  */
92                 FILE *new_fd;
93
94                 new_fd = fopen (obj->file, "r");
95                 if (new_fd == NULL)
96                 {
97                         char errbuf[1024];
98                         ERROR ("cu_tail_readline: open (%s) failed: %s",
99                                         obj->file,
100                                         sstrerror (errno, errbuf,
101                                                 sizeof (errbuf)));
102                         return (-1);
103                 }
104                 
105                 /* If there was no previous file, seek to the end. We don't
106                  * want to read in the entire file, usually. */
107                 if (obj->stat.st_ino == 0)
108                         fseek (new_fd, 0, SEEK_END);
109
110                 if (obj->fd != NULL)
111                         fclose (obj->fd);
112                 obj->fd = new_fd;
113
114         }
115         else if (stat_now.st_size < obj->stat.st_size)
116         {
117                 /*
118                  * Else, if the file was not replaces, but the file was
119                  * truncated, seek to the beginning of the file.
120                  */
121                 assert (obj->fd != NULL);
122                 rewind (obj->fd);
123         }
124
125         status = 0;
126         if (fgets (buf, buflen, obj->fd) == NULL)
127         {
128                 if (feof (obj->fd) == 0)
129                         buf[0] = '\0';
130                 else /* an error occurred */
131                         status = -1;
132         }
133
134         if (status == 0)
135                 memcpy (&obj->stat, &stat_now, sizeof (struct stat));   
136         
137         return (status);
138 } /* int cu_tail_readline */
139
140 int cu_tail_read (cu_tail_t *obj, char *buf, int buflen, tailfunc_t *callback,
141                 void *data)
142 {
143         int status;
144
145         while ((status = cu_tail_readline (obj, buf, buflen)) == 0)
146         {
147                 /* check for EOF */
148                 if (buf[0] == '\0')
149                         break;
150
151                 status = callback (data, buf, buflen);
152                 if (status != 0)
153                         break;
154         }
155
156         return status;
157 } /* int cu_tail_read */