Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/procparser.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014 Davide Madrisan <[email protected]>
5 *
6 * A parser for the /proc files /proc/meminfo and /proc/vmstat
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE /* activate extra prototypes for glibc */
23 #endif
24
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include "string-macros.h"
36 #include "messages.h"
37 #include "procparser.h"
38 #include "xalloc.h"
39
40 static int
41 compare_proc_table_structs (const void *a, const void *b)
42 {
43 return strcmp (((const proc_table_struct *) a)->name,
44 ((const proc_table_struct *) b)->name);
45 }
46
47 void
48 procparser (const char *filename, const proc_table_struct *proc_table,
49 int proc_table_count, char separator)
50 {
51 char namebuf[32]; /* big enough to hold any row name */
52 proc_table_struct findme = { namebuf, NULL };
53 proc_table_struct *found;
54 char *line = NULL;
55 FILE *fp;
56 size_t len = 0;
57 ssize_t chread;
58
59 #if __SIZEOF_LONG__ == 4
60 unsigned long long slotll;
61 #endif
62
63 if ((fp = fopen (filename, "r")) == NULL)
64 plugin_error (STATE_UNKNOWN, errno, "error: cannot read %s", filename);
65
66 while ((chread = getline (&line, &len, fp)) != -1)
67 {
68 char *head = line;
69 char *tail = strchr (line, separator);
70 if (!tail)
71 continue;
72 *tail = '\0';
73 if (strlen (head) >= sizeof (namebuf))
74 continue;
75 strcpy (namebuf, head);
76 found = bsearch (&findme, proc_table, proc_table_count,
77 sizeof (proc_table_struct),
78 compare_proc_table_structs);
79 head = tail + 1;
80 if (!found)
81 continue;
82
83 #if __SIZEOF_LONG__ == 4
84 /* A 32 bit kernel would have already truncated the value, a 64 bit kernel
85 * doesn't need to. Truncate here to let 32 bit programs to continue to get
86 * truncated values. It's that or change the API for a larger data type.
87 */
88 slotll = strtoull (head, &tail, 10);
89 *(found->slot) = (unsigned long) slotll;
90 #else
91 *(found->slot) = strtoul (head, &tail, 10);
92 #endif
93 }
94
95 free (line);
96 }
97
98 int
99 linelookup (char *line, char *pattern, char **value)
100 {
101 char *p, *v;
102 int len = strlen (pattern);
103
104 if (!*line)
105 return 0;
106
107 /* pattern */
108 if (strncmp (line, pattern, len))
109 return 0;
110
111 /* white spaces */
112 for (p = line + len; isspace (*p); p++);
113
114 /* separator */
115 if (*p != ':')
116 return 0;
117
118 /* white spaces */
119 for (++p; isspace (*p); p++);
120
121 /* value */
122 if (!*p)
123 return 0;
124 v = p;
125
126 /* end of value */
127 len = strlen (line) - 1;
128 for (p = line + len; isspace (*(p - 1)); p--);
129 *p = '\0';
130
131 *value = xstrdup (v);
132 return 1;
133 }