Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 1 | /* 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 | |
| 37 | struct osysmon_file { |
| 38 | struct llist_head list; |
| 39 | struct { |
| 40 | const char *name; |
| 41 | const char *path; |
| 42 | } cfg; |
| 43 | }; |
| 44 | |
| 45 | static 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 | |
| 56 | static 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 | |
| 71 | static void osysmon_file_destroy(struct osysmon_file *of) |
| 72 | { |
| 73 | llist_del(&of->list); |
| 74 | talloc_free(of); |
| 75 | } |
| 76 | |
| 77 | static void osysmon_file_read(struct osysmon_file *of, struct value_node *parent) |
| 78 | { |
| 79 | char buf[512]; |
Stefan Sperling | 908d3cc | 2018-11-27 11:47:41 +0100 | [diff] [blame] | 80 | char *s, *nl; |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 81 | FILE *f; |
| 82 | |
| 83 | f = fopen(of->cfg.path, "r"); |
| 84 | if (!f) { |
Max | f41973e | 2019-01-25 18:40:11 +0100 | [diff] [blame^] | 85 | value_node_add(parent, of->cfg.name, "<NOTFOUND>"); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 86 | return; |
| 87 | } |
Stefan Sperling | 908d3cc | 2018-11-27 11:47:41 +0100 | [diff] [blame] | 88 | s = fgets(buf, sizeof(buf), f); |
| 89 | fclose(f); |
| 90 | if (s == NULL) { |
Max | f41973e | 2019-01-25 18:40:11 +0100 | [diff] [blame^] | 91 | value_node_add(parent, of->cfg.name, "<EMPTY>"); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | buf[sizeof(buf)-1] = '\0'; |
| 95 | while ((nl = strrchr(buf, '\n'))) |
| 96 | *nl = '\0'; |
Max | f41973e | 2019-01-25 18:40:11 +0100 | [diff] [blame^] | 97 | value_node_add(parent, of->cfg.name, buf); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /*********************************************************************** |
| 101 | * VTY |
| 102 | ***********************************************************************/ |
| 103 | |
| 104 | #define FILE_STR "Configure a file to be monitored/watched\n" |
| 105 | DEFUN(cfg_file, cfg_file_cmd, |
| 106 | "file NAME PATH", |
| 107 | FILE_STR "Name of this file-watcher\n" "Path of file in filesystem\n") |
| 108 | { |
| 109 | struct osysmon_file *of; |
| 110 | of = osysmon_file_add(argv[0], argv[1]); |
| 111 | if (!of) { |
| 112 | vty_out(vty, "Couldn't add file-watcher, maybe it exists?%s", VTY_NEWLINE); |
| 113 | return CMD_WARNING; |
| 114 | } |
| 115 | return CMD_SUCCESS; |
| 116 | } |
| 117 | |
| 118 | DEFUN(cfg_no_file, cfg_no_file_cmd, |
| 119 | "no file NAME", |
| 120 | NO_STR FILE_STR "Name of this file-watcher\n") |
| 121 | { |
| 122 | struct osysmon_file *of; |
| 123 | of = osysmon_file_find(argv[0]); |
| 124 | if (!of) { |
| 125 | vty_out(vty, "Cannot find file-watcher for '%s'%s", argv[0], VTY_NEWLINE); |
| 126 | return CMD_WARNING; |
| 127 | } |
| 128 | osysmon_file_destroy(of); |
| 129 | return CMD_SUCCESS; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | static void osysmon_file_vty_init(void) |
| 134 | { |
| 135 | install_element(CONFIG_NODE, &cfg_file_cmd); |
| 136 | install_element(CONFIG_NODE, &cfg_no_file_cmd); |
| 137 | } |
| 138 | |
| 139 | /*********************************************************************** |
| 140 | * Runtime Code |
| 141 | ***********************************************************************/ |
| 142 | |
| 143 | /* called once on startup before config file parsing */ |
| 144 | int osysmon_file_init() |
| 145 | { |
| 146 | osysmon_file_vty_init(); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* called periodically */ |
| 151 | int osysmon_file_poll(struct value_node *parent) |
| 152 | { |
| 153 | struct value_node *vn_file; |
| 154 | struct osysmon_file *of; |
| 155 | |
Max | f41973e | 2019-01-25 18:40:11 +0100 | [diff] [blame^] | 156 | vn_file = value_node_add(parent, "file", NULL); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 157 | |
| 158 | llist_for_each_entry(of, &g_oss->files, list) |
| 159 | osysmon_file_read(of, vn_file); |
| 160 | |
| 161 | return 0; |
| 162 | } |