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