Translation update done using Pootle.
[gammu.git] / smsd / monitor.c
blob3b39486b1f2b9099139c2e970858e501416ee0f8
1 /**
2 * SMSD message monitor program
3 */
4 /* Copyright (c) 2009 - 2011 Michal Cihar <michal@cihar.com> */
5 /* Licensend under GNU GPL 2 */
7 #include <gammu-smsd.h>
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <signal.h>
11 #ifndef WIN32
12 #include <unistd.h>
13 #endif
14 #ifdef HAVE_GETOPT_LONG
15 #include <getopt.h>
16 #endif
18 #include "common.h"
20 #if !defined(WIN32) && (defined(HAVE_GETOPT) || defined(HAVE_GETOPT_LONG))
21 #define HAVE_DEFAULT_CONFIG
22 const char default_config[] = "/etc/gammu-smsdrc";
23 #endif
25 volatile gboolean terminate = FALSE;
26 int delay_seconds = 20;
27 int limit_loops = -1;
28 gboolean compact = FALSE;
30 void smsd_interrupt(int signum)
32 terminate = TRUE;
35 NORETURN void version(void)
37 printf("Gammu-smsd-monitor version %s\n", GAMMU_VERSION);
38 printf("Built %s on %s using %s\n", __TIME__, __DATE__, GetCompiler());
39 printf("Compiled in features:\n");
40 printf("OS support:\n");
41 #ifdef HAVE_SHM
42 printf(" - %s\n", "SHM");
43 #endif
44 #ifdef HAVE_GETOPT
45 printf(" - %s\n", "GETOPT");
46 #endif
47 #ifdef HAVE_GETOPT_LONG
48 printf(" - %s\n", "GETOPT_LONG");
49 #endif
50 printf("Backend services:\n");
51 printf(" - %s\n", "NULL");
52 printf(" - %s\n", "FILES");
53 #ifdef HAVE_MYSQL_MYSQL_H
54 printf(" - %s\n", "MYSQL");
55 #endif
56 #ifdef HAVE_POSTGRESQL_LIBPQ_FE_H
57 printf(" - %s\n", "POSTGRESQL");
58 #endif
59 #ifdef LIBDBI_FOUND
60 printf(" - %s\n", "DBI");
61 #endif
62 #ifdef ODBC_FOUND
63 printf(" - %s\n", "ODBC");
64 #endif
65 printf("\n");
66 printf("Copyright (C) 2003 - 2011 Michal Cihar <michal@cihar.com> and other authors.\n");
67 printf("\n");
68 printf("License GPLv2: GNU GPL version 2 <http://creativecommons.org/licenses/GPL/2.0/>.\n");
69 printf("This is free software: you are free to change and redistribute it.\n");
70 printf("There is NO WARRANTY, to the extent permitted by law.\n");
71 printf("\n");
72 printf("Check <http://wammu.eu/gammu/> for updates.\n");
73 printf("\n");
74 exit(0);
77 #ifdef HAVE_GETOPT_LONG
78 #define print_option(name, longname, help) \
79 printf("-%s / --%s - %s\n", name, longname, help);
80 #define print_option_param(name, longname, paramname, help) \
81 printf("-%s / --%s %s - %s\n", name, longname, paramname, help);
82 #else
83 #define print_option(name, longname, help) \
84 printf("-%s - %s\n", name, help);
85 #define print_option_param(name, longname, paramname, help) \
86 printf("-%s %s - %s\n", name, paramname, help);
87 #endif
89 void help(void)
91 printf("usage: gammu-smsd-monitor [OPTION]...\n");
92 printf("options:\n");
93 print_option("h", "help", "shows this help");
94 print_option("v", "version", "shows version information");
95 print_option("C", "csv", "CSV output");
96 print_option_param("c", "config", "CONFIG_FILE",
97 "defines path to config file");
98 print_option_param("d", "delay", "DELAY",
99 "delay in seconds between loops");
100 print_option_param("n", "loops", "NUMBER",
101 "delay in seconds between loops");
102 print_option("l", "use-log", "use logging configuration from config file");
103 print_option("L", "no-use-log", "do not use logging configuration from config file (default)");
106 NORETURN void wrong_params(void)
108 fprintf(stderr, "Invalid parameter, use -h for help.\n");
109 exit(1);
112 int process_commandline(int argc, char **argv, SMSD_Parameters * params)
114 int opt;
116 #ifdef HAVE_GETOPT_LONG
117 struct option long_options[] = {
118 {"help", 0, 0, 'h'},
119 {"version", 0, 0, 'v'},
120 {"config", 1, 0, 'c'},
121 {"delay", 1, 0, 'd'},
122 {"loops", 1, 0, 'n'},
123 {"use-log", 0, 0, 'l'},
124 {"no-use-log", 0, 0, 'L'},
125 {0, 0, 0, 0}
127 int option_index;
129 while ((opt =
130 getopt_long(argc, argv, "+hvc:d:n:ClL", long_options,
131 &option_index)) != -1) {
132 #elif defined(HAVE_GETOPT)
133 while ((opt = getopt(argc, argv, "+hvc:d:n:ClL")) != -1) {
134 #else
135 /* Poor mans getopt replacement */
136 int i, optind = -1;
138 #define optarg argv[++i]
140 for (i = 1; i < argc; i++) {
141 if (strlen(argv[i]) != 2 || argv[i][0] != '-') {
142 wrong_params();
144 opt = argv[i][1];
145 #endif
146 switch (opt) {
147 case 'c':
148 params->config_file = optarg;
149 break;
150 case 'v':
151 version();
152 break;
153 case 'C':
154 compact = TRUE;
155 break;
156 case 'd':
157 delay_seconds = atoi(optarg);
158 break;
159 case 'n':
160 limit_loops = atoi(optarg);
161 break;
162 case 'l':
163 params->use_log = TRUE;
164 break;
165 case 'L':
166 params->use_log = FALSE;
167 break;
168 case '?':
169 wrong_params();
170 case 'h':
171 help();
172 exit(0);
173 default:
174 #if defined(HAVE_GETOPT) || defined(HAVE_GETOPT_LONG)
175 wrong_params();
176 #else
177 optind = 1;
178 #endif
179 break;
181 #if !defined(HAVE_GETOPT) && !defined(HAVE_GETOPT_LONG)
182 if (optind != -1) break;
183 #endif
186 return optind;
190 #ifndef WIN32
191 #endif
193 int main(int argc, char **argv)
195 GSM_Error error;
196 GSM_SMSDConfig *config;
197 GSM_SMSDStatus status;
198 const char program_name[] = "gammu-smsd-monitor";
199 SMSD_Parameters params = {
200 NULL,
201 NULL,
204 NULL,
205 NULL,
206 FALSE,
207 FALSE,
208 FALSE,
209 FALSE,
210 FALSE,
211 FALSE,
212 FALSE,
218 * We don't need gettext, but need to set locales so that
219 * charset conversion works.
221 GSM_InitLocales(NULL);
223 process_commandline(argc, argv, &params);
225 if (params.config_file == NULL) {
226 #ifdef HAVE_DEFAULT_CONFIG
227 params.config_file = default_config;
228 #else
229 fprintf(stderr, "No config file specified!\n");
230 help();
231 exit(1);
232 #endif
235 signal(SIGINT, smsd_interrupt);
236 signal(SIGTERM, smsd_interrupt);
238 config = SMSD_NewConfig(program_name);
239 assert(config != NULL);
241 error = SMSD_ReadConfig(params.config_file, config, params.use_log);
242 if (error != ERR_NONE) {
243 printf("Failed to read config: %s\n", GSM_ErrorString(error));
244 SMSD_FreeConfig(config);
245 return 2;
248 while (!terminate && (limit_loops == -1 || limit_loops-- > 0)) {
249 error = SMSD_GetStatus(config, &status);
250 if (error != ERR_NONE) {
251 printf("Failed to get status: %s\n", GSM_ErrorString(error));
252 SMSD_FreeConfig(config);
253 return 3;
255 if (compact) {
256 printf("%s;%s;%s;%d;%d;%d;%d;%d\n",
257 status.Client,
258 status.PhoneID,
259 status.IMEI,
260 status.Sent,
261 status.Received,
262 status.Failed,
263 status.Charge.BatteryPercent,
264 status.Network.SignalPercent);
265 } else {
266 printf("Client: %s\n", status.Client);
267 printf("PhoneID: %s\n", status.PhoneID);
268 printf("IMEI: %s\n", status.IMEI);
269 printf("Sent: %d\n", status.Sent);
270 printf("Received: %d\n", status.Received);
271 printf("Failed: %d\n", status.Failed);
272 printf("BatterPercent: %d\n", status.Charge.BatteryPercent);
273 printf("NetworkSignal: %d\n", status.Network.SignalPercent);
274 printf("\n");
276 sleep(delay_seconds);
279 SMSD_FreeConfig(config);
281 return 0;
284 /* How should editor hadle tabs in this file? Add editor commands here.
285 * vim: noexpandtab sw=8 ts=8 sts=8: