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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
/***********************************************************************
*
* md-mx-ctrl.c
*
* Command-line utility for talking to mimedefang-multiplexor directly
*
* Copyright (C) 2002-2005 by Roaring Penguin Software Inc.
*
* http://www.roaringpenguin.com
*
* This program may be distributed under the terms of the GNU General
* Public License, Version 2, or (at your option) any later version.
*
***********************************************************************/
#include "config.h"
#define SMALLBUF 16384
#include <ctype.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
char cmdbuf[SMALLBUF+1];
int read_stdin = 0;
FILE *errfp;
static int
percent_encode(unsigned char *in,
unsigned char *out,
int outlen);
static int process(char const *sock, char const *cmd);
/**********************************************************************
* %FUNCTION: percent_decode
* %ARGUMENTS:
* buf -- a buffer with percent-encoded data
* %RETURNS:
* Nothing
* %DESCRIPTION:
* Decodes buf IN PLACE.
***********************************************************************/
static void
percent_decode(unsigned char *buf)
{
unsigned char *in = buf;
unsigned char *out = buf;
unsigned int val;
while(*in) {
if (*in == '%' && isxdigit(*(in+1)) && isxdigit(*(in+2))) {
sscanf((char *) in+1, "%2x", &val);
*out++ = (unsigned char) val;
in += 3;
continue;
}
*out++ = *in++;
}
/* Copy terminator */
*out = 0;
}
/**********************************************************************
* %FUNCTION: MXCommand
* %ARGUMENTS:
* sockname -- multiplexor socket name
* cmd -- command to send
* buf -- buffer for reply
* len -- length of buffer
* %RETURNS:
* 0 if all went well, -1 on error.
* %DESCRIPTION:
* Sends a command to the multiplexor and reads the answer back.
***********************************************************************/
static int
MXCommand(char const *sockname,
char const *cmd,
char *buf,
int len)
{
int fd;
struct sockaddr_un addr;
int nleft, nwritten, nread;
int n;
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(errfp, "ERROR MXCommand: socket: %s\n", strerror(errno));
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
strncpy(addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
if (errno == EPERM || errno == EACCES) {
fprintf(errfp, "ERROR MXCommand: connect: %s\n", strerror(errno));
} else {
fprintf(errfp,
"ERROR MXCommand: connect: %s: Is multiplexor running?\n",
strerror(errno));
}
close(fd);
return -1;
}
nwritten = 0;
nleft = strlen(cmd);
n = 0;
while(nleft) {
n = write(fd, cmd+nwritten, nleft);
if (n < 0) {
if (errno == EINTR) continue;
break;
}
nwritten += n;
nleft -= n;
}
if (n < 0) {
fprintf(errfp, "ERROR MXCommand: write: %s: Is multiplexor running?\n",
strerror(errno));
close(fd);
return -1;
}
/* Now read the answer */
nread = 0;
nleft = len;
while(nleft) {
n = read(fd, buf+nread, nleft);
if (n <= 0) {
if (n < 0 && errno == EINTR) continue;
break;
}
nread += n;
nleft -= n;
}
if (n < 0) {
fprintf(errfp, "ERROR MXCommand: read: %s: Is multiplexor running?\n",
strerror(errno));
close(fd);
return -1;
}
buf[nread] = 0;
close(fd);
return 0;
}
static void
buildCmd(int argc, char *argv[], char *outbuf, int buflen)
{
int i;
int k;
int len = 0;
for (i=0; i<argc; i++) {
k = percent_encode((unsigned char *) argv[i], (unsigned char *) outbuf+len, buflen-len);
len += k;
if (len >= buflen-1) {
outbuf[buflen-1] = 0;
return;
}
if (i < argc-1) {
outbuf[len] = ' ';
len++;
}
}
outbuf[len++] = '\n';
outbuf[len] = 0;
}
static int
percent_encode(unsigned char *in,
unsigned char *out,
int outlen)
{
unsigned char tmp[8];
int nwritten = 0;
unsigned char c;
if (outlen <= 0) {
return 0;
}
if (outlen == 1) {
*out = 0;
return 0;
}
/* Do real work */
while((c = *in++) != 0) {
if (c <= 32 || c > 126 || c == '%' || c == '\\' || c == '\'' || c == '"') {
if (nwritten >= outlen-3) {
break;
}
sprintf((char *) tmp, "%%%02X", (unsigned int) c);
*out++ = tmp[0];
*out++ = tmp[1];
*out++ = tmp[2];
nwritten += 3;
} else {
*out++ = c;
nwritten++;
}
if (nwritten >= outlen-1) {
break;
}
}
out[nwritten] = 0;
return nwritten;
}
static int
doCmd(char const *sock, char const *cmd, int decode)
{
char ans[SMALLBUF];
if (MXCommand(sock, cmd, ans, sizeof(ans)) < 0) {
return EXIT_FAILURE;
}
if (decode) {
percent_decode((unsigned char *)ans);
}
printf("%s", ans);
return EXIT_SUCCESS;
}
static int
doStatus(char const *sock)
{
char ans[4096];
char *s;
int i, l;
if (MXCommand(sock, "status\n", ans, sizeof(ans)) < 0) {
return EXIT_FAILURE;
}
if (*ans != 'I' && *ans != 'K' && *ans != 'S' && *ans != 'B') {
fprintf(errfp, "ERROR %s", ans);
return EXIT_FAILURE;
}
/* Chop off message and activation count */
s = ans;
while (*s && *s != ' ') s++;
*s = 0;
l = strlen(ans);
printf("Max slaves: %d\n", l);
for (i=0; i<l; i++) {
printf("Slave %d: ", i);
switch(ans[i]) {
case 'I':
printf("idle\n");
break;
case 'K':
printf("killed\n");
break;
case 'S':
printf("stopped\n");
break;
case 'B':
printf("busy\n");
break;
default:
printf("unknown state '%c'\n", ans[i]);
}
}
return EXIT_SUCCESS;
}
#define BARLEN 25
#define QBARLEN 20
static int
doBarStatus(char const *sock)
{
char ans[4096];
char *s;
int i;
int nbusy, ntotal, nqueued, maxqueue;
int nmsgs;
if (MXCommand(sock, "status\n", ans, sizeof(ans)) < 0) {
return EXIT_FAILURE;
}
if (*ans != 'I' && *ans != 'K' && *ans != 'S' && *ans != 'B') {
fprintf(errfp, "ERROR %s", ans);
return EXIT_FAILURE;
}
nbusy = 0;
ntotal = 0;
nqueued = 0;
maxqueue = 0;
/* Count number of busy slaves */
s = ans;
while (*s && *s != ' ') {
if (*s == 'B') {
nbusy++;
}
ntotal++;
s++;
}
if (ntotal == 0) {
printf("Max 0 slaves ??\n");
return EXIT_SUCCESS;
}
/* Get num queue and max queued */
sscanf(s, "%d %d %d %d", &nmsgs, &i, &maxqueue, &nqueued);
/* Print busy graph */
printf("%3d/%d ", nbusy, ntotal);
if (ntotal <= BARLEN) {
for (i=0; i<ntotal; i++) {
if (i < nbusy) {
printf("B");
} else {
printf(".");
}
}
} else {
for (i=0; i<BARLEN; i++) {
if (i < ( (float) nbusy / (float) ntotal) * (float) BARLEN) {
printf("B");
} else {
printf(".");
}
}
}
if (maxqueue == 0) {
printf(" %d\n", nmsgs);
return EXIT_SUCCESS;
}
printf(" %3d/%d ", nqueued, maxqueue);
if (maxqueue <= QBARLEN) {
for (i=0; i<maxqueue; i++) {
if (i < nqueued) {
printf("Q");
} else {
printf(".");
}
}
} else {
for (i=0; i<QBARLEN; i++) {
if (i < (float) nqueued / (float) maxqueue * (float) QBARLEN) {
printf("Q");
} else {
printf(".");
}
}
}
printf(" %d\n", nmsgs);
return EXIT_SUCCESS;
}
static int
doHLoad(char const *rawcmd,
char const *msgstr,
char const *scanstr,
char const *sock)
{
char ans[4096];
int msgs_1, msgs_4, msgs_12, msgs_24;
int secs_1, secs_4, secs_12, secs_24;
double avg_1, avg_4, avg_12, avg_24;
double mps_1, mps_4, mps_12, mps_24;
double ams_1, ams_4, ams_12, ams_24;
char persec[256];
char ms[256];
sprintf(persec, "%s/Sec", msgstr);
sprintf(ms, "Avg ms/%s", scanstr);
if (MXCommand(sock, rawcmd, ans, sizeof(ans)) < 0) {
return EXIT_FAILURE;
}
sscanf(ans, "%d %d %d %d %lf %lf %lf %lf %lf %lf %lf %lf %d %d %d %d",
&msgs_1, &msgs_4, &msgs_12, &msgs_24,
&avg_1, &avg_4, &avg_12, &avg_24,
&ams_1, &ams_4, &ams_12, &ams_24,
&secs_1, &secs_4, &secs_12, &secs_24);
if (secs_1) mps_1 = (double) msgs_1 / (double) secs_1;
else mps_1 = 0;
if (secs_4) mps_4 = (double) msgs_4 / (double) secs_4;
else mps_4 = 0;
if (secs_12) mps_12 = (double) msgs_12 / (double) secs_12;
else mps_12 = 0;
if (secs_24) mps_24 = (double) msgs_24 / (double) secs_24;
else mps_24 = 0;
printf("%6s %17s %15s %15s %s\n", "Load", msgstr, persec, ms, " Avg Busy Slaves");
printf("%6s %15d %15.2f %15.1f %15.2f\n", " 1h", msgs_1, mps_1, ams_1, avg_1);
printf("%6s %15d %15.2f %15.1f %15.2f\n", " 4h", msgs_4, mps_4, ams_4, avg_4);
printf("%6s %15d %15.2f %15.1f %15.2f\n", "12h", msgs_12, mps_12, ams_12, avg_12);
printf("%6s %15d %15.2f %15.1f %15.2f\n", "24h", msgs_24, mps_24, ams_24, avg_24);
return EXIT_SUCCESS;
}
static int
doLoad(char const *rawcmd,
char const *msgstr,
char const *scanstr,
char const *sock)
{
char ans[4096];
int msgs_0, msgs_1, msgs_5, msgs_10;
double avg_0, avg_1, avg_5, avg_10;
double mps_0, mps_1, mps_5, mps_10;
double ams_0, ams_1, ams_5, ams_10;
char persec[256];
char ms[256];
sprintf(persec, "%s/Sec", msgstr);
sprintf(ms, "Avg ms/%s", scanstr);
if (MXCommand(sock, rawcmd, ans, sizeof(ans)) < 0) {
return EXIT_FAILURE;
}
sscanf(ans, "%d %d %d %d %lf %lf %lf %lf %lf %lf %lf %lf",
&msgs_0, &msgs_1, &msgs_5, &msgs_10,
&avg_0, &avg_1, &avg_5, &avg_10,
&ams_0, &ams_1, &ams_5, &ams_10);
mps_0 = (double) msgs_0 / 10.0;
mps_1 = (double) msgs_1 / 60.0;
mps_5 = (double) msgs_5 / (5*60.0);
mps_10 = (double) msgs_10 / (10*60.0);
printf("%6s %17s %15s %15s %s\n", "Load", msgstr, persec, ms, " Avg Busy Slaves");
printf("%6s %15d %15.2f %15.1f %15.2f\n", "10 Sec", msgs_0, mps_0, ams_0, avg_0);
printf("%6s %15d %15.2f %15.1f %15.2f\n", "1 Min", msgs_1, mps_1, ams_1, avg_1);
printf("%6s %15d %15.2f %15.1f %15.2f\n", "5 Min", msgs_5, mps_5, ams_5, avg_5);
printf("%6s %15d %15.2f %15.1f %15.2f\n", "10 Min", msgs_10, mps_10, ams_10, avg_10);
return EXIT_SUCCESS;
}
static void
usage(char const *sock)
{
char ans[SMALLBUF];
fprintf(stderr, "Usage: md-mx-ctrl [options] command\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, "-h -- Display help\n");
fprintf(stderr, "-s path -- Specify path to multiplexor socket\n");
fprintf(stderr, "-i -- Read commands from stdin\n");
if (!sock) {
sock = SPOOLDIR "/mimedefang-multiplexor.sock";
}
if (MXCommand(sock, "help\n", ans, sizeof(ans)) >= 0) {
fprintf(stderr, "\nPossible commands:\n%s", ans);
}
}
int
main(int argc, char *argv[])
{
int c;
char *sock = NULL;
errfp = stderr;
while ((c = getopt(argc, argv, "hs:i")) != -1) {
switch(c) {
case 'i':
read_stdin = 1;
errfp = stdout;
break;
case 'h':
usage(sock);
exit(EXIT_SUCCESS);
case 's':
if (sock) free(sock);
sock = strdup(optarg);
if (!sock) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
break;
default:
usage(sock);
exit(EXIT_FAILURE);
}
}
if (!read_stdin && !argv[optind]) {
usage(sock);
exit(EXIT_FAILURE);
}
if (!sock) {
sock = SPOOLDIR "/mimedefang-multiplexor.sock";
}
if (read_stdin) {
while(fgets(cmdbuf, SMALLBUF, stdin)) {
process(sock, cmdbuf);
fflush(stdout);
}
} else {
buildCmd(argc-optind, argv+optind, cmdbuf, SMALLBUF);
exit(process(sock, cmdbuf));
}
exit(EXIT_SUCCESS);
}
static int
process(char const *sock, char const *cmd) {
if (!strcmp(cmd, "status\n")) {
return doStatus(sock);
} else if (!strcmp(cmd, "barstatus\n")) {
return doBarStatus(sock);
} else if (!strcmp(cmd, "reread\n")) {
return doCmd(sock, "reread\n", 0);
} else if (!strcmp(cmd, "rawstatus\n")) {
return doCmd(sock, "status\n", 0);
} else if (!strcmp(cmd, "msgs\n")) {
return doCmd(sock, "msgs\n", 0);
} else if (!strcmp(cmd, "rawload\n")) {
return doCmd(sock, "load\n", 0);
} else if (!strcmp(cmd, "rawload-relayok\n")) {
return doCmd(sock, "load-relayok\n", 0);
} else if (!strcmp(cmd, "rawload-senderok\n")) {
return doCmd(sock, "load-senderok\n", 0);
} else if (!strcmp(cmd, "rawload-recipok\n")) {
return doCmd(sock, "load-recipok\n", 0);
} else if (!strcmp(cmd, "rawhload\n")) {
return doCmd(sock, "hload\n", 0);
} else if (!strcmp(cmd, "rawhload-relayok\n")) {
return doCmd(sock, "hload-relayok\n", 0);
} else if (!strcmp(cmd, "rawhload-senderok\n")) {
return doCmd(sock, "hload-senderok\n", 0);
} else if (!strcmp(cmd, "rawhload-recipok\n")) {
return doCmd(sock, "hload-recipok\n", 0);
} else if (!strcmp(cmd, "load\n")) {
return doLoad(cmd, "Msgs", "Scan", sock);
} else if (!strcmp(cmd, "load-relayok\n")) {
return doLoad(cmd, "Conn", "Conn", sock);
} else if (!strcmp(cmd, "load-senderok\n")) {
return doLoad(cmd, "MAIL", "MAIL", sock);
} else if (!strcmp(cmd, "load-recipok\n")) {
return doLoad(cmd, "RCPT", "RCPT", sock);
} else if (!strcmp(cmd, "hload\n")) {
return doHLoad(cmd, "Msgs", "Scan", sock);
} else if (!strcmp(cmd, "hload-relayok\n")) {
return doHLoad(cmd, "Conn", "Conn", sock);
} else if (!strcmp(cmd, "hload-senderok\n")) {
return doHLoad(cmd, "MAIL", "MAIL", sock);
} else if (!strcmp(cmd, "hload-recipok\n")) {
return doHLoad(cmd, "RCPT", "RCPT", sock);
} else {
return doCmd(sock, cmd, 1);
}
}
|