blob: 6f826b4888ad4c454a5d4855a30a6bd28eaed172 [file] [log] [blame]
Harald Welte81e20232018-06-04 21:25:56 +02001/* Simple Osmocom System Monitor (osysmon): Support for monitoring files */
2
3/* (C) 2018 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0+
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 2 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, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
22 */
23
24#include <string.h>
25#include <sys/sysinfo.h>
26
27#include <osmocom/vty/vty.h>
28#include <osmocom/vty/command.h>
29
30#include "osysmon.h"
31#include "value_node.h"
32
33/***********************************************************************
34 * Data model
35 ***********************************************************************/
36
37struct osysmon_file {
38 struct llist_head list;
39 struct {
40 const char *name;
41 const char *path;
42 } cfg;
43};
44
45static struct osysmon_file *osysmon_file_find(const char *name)
46{
47 struct osysmon_file *of;
48
49 llist_for_each_entry(of, &g_oss->files, list) {
50 if (!strcmp(of->cfg.name, name))
51 return of;
52 }
53 return NULL;
54}
55
56static struct osysmon_file *osysmon_file_add(const char *name, const char *path)
57{
58 struct osysmon_file *of;
59
60 if (osysmon_file_find(name))
61 return NULL;
62
63 of = talloc_zero(g_oss, struct osysmon_file);
64 OSMO_ASSERT(of);
65 of->cfg.name = talloc_strdup(of, name);
66 of->cfg.path = talloc_strdup(of, path);
67 llist_add_tail(&of->list, &g_oss->files);
68 return of;
69}
70
71static void osysmon_file_destroy(struct osysmon_file *of)
72{
73 llist_del(&of->list);
74 talloc_free(of);
75}
76
77static void osysmon_file_read(struct osysmon_file *of, struct value_node *parent)
78{
79 char buf[512];
80 char *nl;
81 FILE *f;
82
83 f = fopen(of->cfg.path, "r");
84 if (!f) {
85 value_node_add(parent, parent, of->cfg.name, "<NOTFOUND>");
86 return;
87 }
88 if (fgets(buf, sizeof(buf), f) == NULL) {
89 value_node_add(parent, parent, of->cfg.name, "<EMPTY>");
90 return;
91 }
92 buf[sizeof(buf)-1] = '\0';
93 while ((nl = strrchr(buf, '\n')))
94 *nl = '\0';
95 value_node_add(parent, parent, of->cfg.name, buf);
96}
97
98/***********************************************************************
99 * VTY
100 ***********************************************************************/
101
102#define FILE_STR "Configure a file to be monitored/watched\n"
103DEFUN(cfg_file, cfg_file_cmd,
104 "file NAME PATH",
105 FILE_STR "Name of this file-watcher\n" "Path of file in filesystem\n")
106{
107 struct osysmon_file *of;
108 of = osysmon_file_add(argv[0], argv[1]);
109 if (!of) {
110 vty_out(vty, "Couldn't add file-watcher, maybe it exists?%s", VTY_NEWLINE);
111 return CMD_WARNING;
112 }
113 return CMD_SUCCESS;
114}
115
116DEFUN(cfg_no_file, cfg_no_file_cmd,
117 "no file NAME",
118 NO_STR FILE_STR "Name of this file-watcher\n")
119{
120 struct osysmon_file *of;
121 of = osysmon_file_find(argv[0]);
122 if (!of) {
123 vty_out(vty, "Cannot find file-watcher for '%s'%s", argv[0], VTY_NEWLINE);
124 return CMD_WARNING;
125 }
126 osysmon_file_destroy(of);
127 return CMD_SUCCESS;
128}
129
130
131static void osysmon_file_vty_init(void)
132{
133 install_element(CONFIG_NODE, &cfg_file_cmd);
134 install_element(CONFIG_NODE, &cfg_no_file_cmd);
135}
136
137/***********************************************************************
138 * Runtime Code
139 ***********************************************************************/
140
141/* called once on startup before config file parsing */
142int osysmon_file_init()
143{
144 osysmon_file_vty_init();
145 return 0;
146}
147
148/* called periodically */
149int osysmon_file_poll(struct value_node *parent)
150{
151 struct value_node *vn_file;
152 struct osysmon_file *of;
153
154 vn_file = value_node_add(parent, parent, "file", NULL);
155
156 llist_for_each_entry(of, &g_oss->files, list)
157 osysmon_file_read(of, vn_file);
158
159 return 0;
160}