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 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
|
/*
** ATOP - System & Process Monitor
**
** The program 'atop' offers the possibility to view the activity of
** the system on system-level as well as process-level.
**
** This source-file contains functions to read all relevant system-level
** figures.
** ==========================================================================
** Author: Gerlof Langeveld - AT Computing, Nijmegen, Holland
** E-mail: gerlof@ATComputing.nl
** Date: November 1996
** LINUX-port: June 2000
** --------------------------------------------------------------------------
** Copyright (C) 2000-2005 Gerlof Langeveld
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
** --------------------------------------------------------------------------
**
** $Log: photosyst.c,v $
** Revision 1.30 2008/02/25 13:47:00 gerlof
** Experimental code for HTTP statistics.
**
** Revision 1.29 2007/08/17 09:45:44 gerlof
** Experimental: gather info about HTTP statistics.
**
** Revision 1.28 2007/08/16 12:00:49 gerlof
** Add support for atopsar reporting.
** Gather more counters, mainly related to networking.
**
** Revision 1.27 2007/07/03 09:01:56 gerlof
** Support Apache-statistics.
**
** Revision 1.26 2007/02/13 10:32:28 gerlof
** Removal of external declarations.
**
** Revision 1.25 2007/02/13 09:29:57 gerlof
** Removal of external declarations.
**
** Revision 1.24 2007/01/22 14:57:12 gerlof
** Support of special disks used by virtual machines.
**
** Revision 1.23 2007/01/22 08:28:50 gerlof
** Support steal-time from /proc/stat.
**
** Revision 1.22 2006/11/13 13:48:20 gerlof
** Implement load-average counters, context-switches and interrupts.
**
** Revision 1.21 2006/01/30 09:14:16 gerlof
** Extend memory counters (a.o. page scans).
**
** Revision 1.20 2005/10/21 09:50:08 gerlof
** Per-user accumulation of resource consumption.
**
** Revision 1.19 2004/10/28 08:31:23 gerlof
** New counter: vm committed space
**
** Revision 1.18 2004/09/24 10:02:35 gerlof
** Wrong cpu-numbers for system level statistics.
**
** Revision 1.17 2004/05/07 05:27:37 gerlof
** Recognize new disk-names and support of diskname-modification.
**
** Revision 1.16 2004/05/06 09:53:31 gerlof
** Skip statistics of ram-disks.
**
** Revision 1.15 2004/05/06 09:46:14 gerlof
** Ported to kernel-version 2.6.
**
** Revision 1.14 2003/07/08 13:53:21 gerlof
** Cleanup code.
**
** Revision 1.13 2003/07/07 09:27:06 gerlof
** Cleanup code (-Wall proof).
**
** Revision 1.12 2003/06/30 11:30:37 gerlof
** Enlarge counters to 'long long'.
**
** Revision 1.11 2003/06/24 06:21:40 gerlof
** Limit number of system resource lines.
**
** Revision 1.10 2003/01/17 14:23:05 root
** Change-directory to /proc to optimize opening /proc-files
** via relative path-names i.s.o. absolute path-names.
**
** Revision 1.9 2003/01/14 07:50:26 gerlof
** Consider IPv6 counters on IP and UDP level (add them to the IPv4 counters).
**
** Revision 1.8 2002/07/24 11:13:38 gerlof
** Changed to ease porting to other UNIX-platforms.
**
** Revision 1.7 2002/07/11 09:12:41 root
** Parsing of /proc/meminfo made 2.5 proof.
**
** Revision 1.6 2002/07/10 05:00:21 root
** Counters pin/pout renamed to swin/swout (Linux conventions).
**
** Revision 1.5 2002/07/08 09:31:11 gerlof
** *** empty log message ***
**
** Revision 1.4 2002/07/02 07:36:45 gerlof
** *** empty log message ***
**
** Revision 1.3 2002/07/02 07:08:36 gerlof
** Recognize more disk driver types via regular expressions
**
** Revision 1.2 2002/01/22 13:40:11 gerlof
** Support for number of cpu's.
**
** Revision 1.1 2001/10/02 10:43:31 gerlof
** Initial revision
**
*/
static const char rcsid[] = "$Id: photosyst.c,v 1.30 2008/02/25 13:47:00 gerlof Exp $";
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <regex.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "atop.h"
#include "photosyst.h"
#define MAXCNT 64
static int isrealdisk(char *, char *, int);
static struct ipv6_stats ipv6_tmp;
static struct icmpv6_stats icmpv6_tmp;
static struct udpv6_stats udpv6_tmp;
struct v6tab {
char *nam;
count_t *val;
};
static struct v6tab v6tab[] = {
{"Ip6InReceives", &ipv6_tmp.Ip6InReceives, },
{"Ip6InHdrErrors", &ipv6_tmp.Ip6InHdrErrors, },
{"Ip6InTooBigErrors", &ipv6_tmp.Ip6InTooBigErrors, },
{"Ip6InNoRoutes", &ipv6_tmp.Ip6InNoRoutes, },
{"Ip6InAddrErrors", &ipv6_tmp.Ip6InAddrErrors, },
{"Ip6InUnknownProtos", &ipv6_tmp.Ip6InUnknownProtos, },
{"Ip6InTruncatedPkts", &ipv6_tmp.Ip6InTruncatedPkts, },
{"Ip6InDiscards", &ipv6_tmp.Ip6InDiscards, },
{"Ip6InDelivers", &ipv6_tmp.Ip6InDelivers, },
{"Ip6OutForwDatagrams", &ipv6_tmp.Ip6OutForwDatagrams, },
{"Ip6OutRequests", &ipv6_tmp.Ip6OutRequests, },
{"Ip6OutDiscards", &ipv6_tmp.Ip6OutDiscards, },
{"Ip6OutNoRoutes", &ipv6_tmp.Ip6OutNoRoutes, },
{"Ip6ReasmTimeout", &ipv6_tmp.Ip6ReasmTimeout, },
{"Ip6ReasmReqds", &ipv6_tmp.Ip6ReasmReqds, },
{"Ip6ReasmOKs", &ipv6_tmp.Ip6ReasmOKs, },
{"Ip6ReasmFails", &ipv6_tmp.Ip6ReasmFails, },
{"Ip6FragOKs", &ipv6_tmp.Ip6FragOKs, },
{"Ip6FragFails", &ipv6_tmp.Ip6FragFails, },
{"Ip6FragCreates", &ipv6_tmp.Ip6FragCreates, },
{"Ip6InMcastPkts", &ipv6_tmp.Ip6InMcastPkts, },
{"Ip6OutMcastPkts", &ipv6_tmp.Ip6OutMcastPkts, },
{"Icmp6InMsgs", &icmpv6_tmp.Icmp6InMsgs, },
{"Icmp6InErrors", &icmpv6_tmp.Icmp6InErrors, },
{"Icmp6InDestUnreachs", &icmpv6_tmp.Icmp6InDestUnreachs, },
{"Icmp6InPktTooBigs", &icmpv6_tmp.Icmp6InPktTooBigs, },
{"Icmp6InTimeExcds", &icmpv6_tmp.Icmp6InTimeExcds, },
{"Icmp6InParmProblems", &icmpv6_tmp.Icmp6InParmProblems, },
{"Icmp6InEchos", &icmpv6_tmp.Icmp6InEchos, },
{"Icmp6InEchoReplies", &icmpv6_tmp.Icmp6InEchoReplies, },
{"Icmp6InGroupMembQueries", &icmpv6_tmp.Icmp6InGroupMembQueries, },
{"Icmp6InGroupMembResponses",
&icmpv6_tmp.Icmp6InGroupMembResponses, },
{"Icmp6InGroupMembReductions",
&icmpv6_tmp.Icmp6InGroupMembReductions, },
{"Icmp6InRouterSolicits", &icmpv6_tmp.Icmp6InRouterSolicits, },
{"Icmp6InRouterAdvertisements",
&icmpv6_tmp.Icmp6InRouterAdvertisements, },
{"Icmp6InNeighborSolicits", &icmpv6_tmp.Icmp6InNeighborSolicits, },
{"Icmp6InNeighborAdvertisements",
&icmpv6_tmp.Icmp6InNeighborAdvertisements, },
{"Icmp6InRedirects", &icmpv6_tmp.Icmp6InRedirects, },
{"Icmp6OutMsgs", &icmpv6_tmp.Icmp6OutMsgs, },
{"Icmp6OutDestUnreachs", &icmpv6_tmp.Icmp6OutDestUnreachs, },
{"Icmp6OutPktTooBigs", &icmpv6_tmp.Icmp6OutPktTooBigs, },
{"Icmp6OutTimeExcds", &icmpv6_tmp.Icmp6OutTimeExcds, },
{"Icmp6OutParmProblems", &icmpv6_tmp.Icmp6OutParmProblems, },
{"Icmp6OutEchoReplies", &icmpv6_tmp.Icmp6OutEchoReplies, },
{"Icmp6OutRouterSolicits", &icmpv6_tmp.Icmp6OutRouterSolicits, },
{"Icmp6OutNeighborSolicits",&icmpv6_tmp.Icmp6OutNeighborSolicits, },
{"Icmp6OutNeighborAdvertisements",
&icmpv6_tmp.Icmp6OutNeighborAdvertisements, },
{"Icmp6OutRedirects", &icmpv6_tmp.Icmp6OutRedirects, },
{"Icmp6OutGroupMembResponses",
&icmpv6_tmp.Icmp6OutGroupMembResponses, },
{"Icmp6OutGroupMembReductions",
&icmpv6_tmp.Icmp6OutGroupMembReductions, },
{"Udp6InDatagrams", &udpv6_tmp.Udp6InDatagrams, },
{"Udp6NoPorts", &udpv6_tmp.Udp6NoPorts, },
{"Udp6InErrors", &udpv6_tmp.Udp6InErrors, },
{"Udp6OutDatagrams", &udpv6_tmp.Udp6OutDatagrams, },
};
static int v6tab_entries = sizeof(v6tab)/sizeof(struct v6tab);
void
photosyst(struct sstat *si)
{
register int i, nr;
count_t cnts[MAXCNT];
float lavg1, lavg5, lavg15;
FILE *fp;
char linebuf[1024], nam[64], origdir[1024];
static char part_stats = 1; /* per-partition statistics ? */
#if HTTPSTATS
static int wwwvalid = 1;
#endif
memset(si, 0, sizeof(struct sstat));
getcwd(origdir, sizeof origdir);
chdir("/proc");
/*
** gather various general statistics from the file /proc/stat and
** store them in binary form
*/
if ( (fp = fopen("stat", "r")) != NULL)
{
while ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
nr = sscanf(linebuf,
"%s %lld %lld %lld %lld %lld %lld %lld "
"%lld %lld %lld %lld %lld %lld %lld %lld ",
nam,
&cnts[0], &cnts[1], &cnts[2], &cnts[3],
&cnts[4], &cnts[5], &cnts[6], &cnts[7],
&cnts[8], &cnts[9], &cnts[10], &cnts[11],
&cnts[12], &cnts[13], &cnts[14]);
if (nr < 2) /* headerline ? --> skip */
continue;
if ( strcmp("cpu", nam) == EQ)
{
si->cpu.all.utime = cnts[0];
si->cpu.all.ntime = cnts[1];
si->cpu.all.stime = cnts[2];
si->cpu.all.itime = cnts[3];
if (nr > 5) /* 2.6 kernel? */
{
si->cpu.all.wtime = cnts[4];
si->cpu.all.Itime = cnts[5];
si->cpu.all.Stime = cnts[6];
if (nr > 8) /* steal support */
si->cpu.all.steal = cnts[7];
}
continue;
}
if ( strncmp("cpu", nam, 3) == EQ)
{
i = atoi(&nam[3]);
si->cpu.cpu[i].cpunr = i;
si->cpu.cpu[i].utime = cnts[0];
si->cpu.cpu[i].ntime = cnts[1];
si->cpu.cpu[i].stime = cnts[2];
si->cpu.cpu[i].itime = cnts[3];
if (nr > 5) /* 2.6 kernel? */
{
si->cpu.cpu[i].wtime = cnts[4];
si->cpu.cpu[i].Itime = cnts[5];
si->cpu.cpu[i].Stime = cnts[6];
if (nr > 8) /* steal support */
si->cpu.cpu[i].steal = cnts[7];
}
si->cpu.nrcpu++;
continue;
}
if ( strcmp("ctxt", nam) == EQ)
{
si->cpu.csw = cnts[0];
continue;
}
if ( strcmp("intr", nam) == EQ)
{
si->cpu.devint = cnts[0];
continue;
}
if ( strcmp("processes", nam) == EQ)
{
si->cpu.nprocs = cnts[0];
continue;
}
if ( strcmp("swap", nam) == EQ) /* < 2.6 */
{
si->mem.swins = cnts[0];
si->mem.swouts = cnts[1];
continue;
}
}
fclose(fp);
if (si->cpu.nrcpu == 0)
si->cpu.nrcpu = 1;
}
/*
** gather loadaverage values from the file /proc/loadavg and
** store them in binary form
*/
if ( (fp = fopen("loadavg", "r")) != NULL)
{
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
if ( sscanf(linebuf, "%f %f %f",
&lavg1, &lavg5, &lavg15) == 3)
{
si->cpu.lavg1 = lavg1;
si->cpu.lavg5 = lavg5;
si->cpu.lavg15 = lavg15;
}
}
fclose(fp);
}
/*
** gather virtual memory statistics from the file /proc/vmstat and
** store them in binary form (>= kernel 2.6)
*/
if ( (fp = fopen("vmstat", "r")) != NULL)
{
int nrfields = 9; /* number of fields to be filled */
while ( fgets(linebuf, sizeof(linebuf), fp) != NULL &&
nrfields > 0)
{
nr = sscanf(linebuf, "%s %lld", nam, &cnts[0]);
if (nr < 2) /* headerline ? --> skip */
continue;
if ( strcmp("pswpin", nam) == EQ)
{
si->mem.swins = cnts[0];
nrfields--;
continue;
}
if ( strcmp("pswpout", nam) == EQ)
{
si->mem.swouts = cnts[0];
nrfields--;
continue;
}
if ( strcmp("allocstall", nam) == EQ)
{
si->mem.allocstall = cnts[0];
nrfields--;
continue;
}
if ( strncmp("pgscan_", nam, 7) == EQ)
{
si->mem.pgscans += cnts[0];
nrfields--;
continue;
}
}
fclose(fp);
}
/*
** gather memory-related statistics from the file /proc/meminfo and
** store them in binary form
**
** in the file /proc/meminfo a 2.4 kernel starts with two lines
** headed by the strings "Mem:" and "Swap:" containing all required
** fields, except proper value for page cache
** if these lines are present we try to skip parsing the rest
** of the lines; if these lines are not present we should get the
** required field from other lines
*/
si->mem.physmem = (count_t)-1;
si->mem.freemem = (count_t)-1;
si->mem.buffermem = (count_t)-1;
si->mem.cachemem = (count_t)-1;
si->mem.slabmem = (count_t) 0;
si->mem.totswap = (count_t)-1;
si->mem.freeswap = (count_t)-1;
si->mem.committed = (count_t) 0;
if ( (fp = fopen("meminfo", "r")) != NULL)
{
int nrfields = 9; /* number of fields to be filled */
while ( fgets(linebuf, sizeof(linebuf), fp) != NULL &&
nrfields > 0)
{
nr = sscanf(linebuf,
"%s %lld %lld %lld %lld %lld %lld %lld "
"%lld %lld %lld\n",
nam,
&cnts[0], &cnts[1], &cnts[2], &cnts[3],
&cnts[4], &cnts[5], &cnts[6], &cnts[7],
&cnts[8], &cnts[9]);
if (nr < 2) /* headerline ? --> skip */
continue;
if ( strcmp("Mem:", nam) == EQ)
{
si->mem.physmem = cnts[0] / pagesize;
si->mem.freemem = cnts[2] / pagesize;
si->mem.buffermem = cnts[4] / pagesize;
nrfields -= 3;
}
else if ( strcmp("Swap:", nam) == EQ)
{
si->mem.totswap = cnts[0] / pagesize;
si->mem.freeswap = cnts[2] / pagesize;
nrfields -= 2;
}
else if (strcmp("Cached:", nam) == EQ)
{
if (si->mem.cachemem == (count_t)-1)
{
si->mem.cachemem =
cnts[0]*1024/pagesize;
nrfields--;
}
}
else if (strcmp("MemTotal:", nam) == EQ)
{
if (si->mem.physmem == (count_t)-1)
{
si->mem.physmem =
cnts[0]*1024/pagesize;
nrfields--;
}
}
else if (strcmp("MemFree:", nam) == EQ)
{
if (si->mem.freemem == (count_t)-1)
{
si->mem.freemem =
cnts[0]*1024/pagesize;
nrfields--;
}
}
else if (strcmp("Buffers:", nam) == EQ)
{
if (si->mem.buffermem == (count_t)-1)
{
si->mem.buffermem =
cnts[0]*1024/pagesize;
nrfields--;
}
}
else if (strcmp("SwapTotal:", nam) == EQ)
{
if (si->mem.totswap == (count_t)-1)
{
si->mem.totswap =
cnts[0]*1024/pagesize;
nrfields--;
}
}
else if (strcmp("SwapFree:", nam) == EQ)
{
if (si->mem.freeswap == (count_t)-1)
{
si->mem.freeswap =
cnts[0]*1024/pagesize;
nrfields--;
}
}
else if (strcmp("Slab:", nam) == EQ)
{
si->mem.slabmem = cnts[0]*1024/pagesize;
nrfields--;
}
else if (strcmp("Committed_AS:", nam) == EQ)
{
si->mem.committed = cnts[0]*1024/
pagesize;
nrfields--;
}
else if (strcmp("CommitLimit:", nam) == EQ)
{
si->mem.commitlim = cnts[0]*1024/
pagesize;
nrfields--;
}
}
fclose(fp);
}
/*
** gather network-related statistics
** - interface stats from the file /proc/net/dev
** - IPv4 stats from the file /proc/net/snmp
** - IPv6 stats from the file /proc/net/snmp6
*/
/*
** interface statistics
*/
if ( (fp = fopen("net/dev", "r")) != NULL)
{
char *cp;
i = 0;
while ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
if ( (cp = strchr(linebuf, ':')) != NULL)
*cp = ' '; /* substitute ':' by space */
nr = sscanf(linebuf,
"%15s %lld %lld %lld %lld %lld %lld %lld "
"%lld %lld %lld %lld %lld %lld %lld %lld "
"%lld\n",
si->intf.intf[i].name,
&(si->intf.intf[i].rbyte),
&(si->intf.intf[i].rpack),
&(si->intf.intf[i].rerrs),
&(si->intf.intf[i].rdrop),
&(si->intf.intf[i].rfifo),
&(si->intf.intf[i].rframe),
&(si->intf.intf[i].rcompr),
&(si->intf.intf[i].rmultic),
&(si->intf.intf[i].sbyte),
&(si->intf.intf[i].spack),
&(si->intf.intf[i].serrs),
&(si->intf.intf[i].sdrop),
&(si->intf.intf[i].sfifo),
&(si->intf.intf[i].scollis),
&(si->intf.intf[i].scarrier),
&(si->intf.intf[i].scompr));
if (nr == 17) /* skip header & lines without stats */
{
if (++i >= MAXINTF-1)
break;
}
}
si->intf.intf[i].name[0] = '\0'; /* set terminator for table */
si->intf.nrintf = i;
fclose(fp);
}
/*
** IP version 4 statistics
*/
if ( (fp = fopen("net/snmp", "r")) != NULL)
{
while ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
nr = sscanf(linebuf,
"%s %lld %lld %lld %lld %lld %lld %lld %lld %lld "
"%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld "
"%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld "
"%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld "
"%lld\n",
nam,
&cnts[0], &cnts[1], &cnts[2], &cnts[3],
&cnts[4], &cnts[5], &cnts[6], &cnts[7],
&cnts[8], &cnts[9], &cnts[10], &cnts[11],
&cnts[12], &cnts[13], &cnts[14], &cnts[15],
&cnts[16], &cnts[17], &cnts[18], &cnts[19],
&cnts[20], &cnts[21], &cnts[22], &cnts[23],
&cnts[24], &cnts[25], &cnts[26], &cnts[27],
&cnts[28], &cnts[29], &cnts[30], &cnts[31],
&cnts[32], &cnts[33], &cnts[34], &cnts[35],
&cnts[36], &cnts[37], &cnts[38], &cnts[39]);
if (nr < 2) /* headerline ? --> skip */
continue;
if ( strcmp("Ip:", nam) == 0)
{
memcpy(&si->net.ipv4, cnts,
sizeof si->net.ipv4);
continue;
}
if ( strcmp("Icmp:", nam) == 0)
{
memcpy(&si->net.icmpv4, cnts,
sizeof si->net.icmpv4);
continue;
}
if ( strcmp("Tcp:", nam) == 0)
{
memcpy(&si->net.tcp, cnts,
sizeof si->net.tcp);
continue;
}
if ( strcmp("Udp:", nam) == 0)
{
memcpy(&si->net.udpv4, cnts,
sizeof si->net.udpv4);
continue;
}
}
fclose(fp);
}
/*
** IP version 6 statistics
*/
memset(&ipv6_tmp, 0, sizeof ipv6_tmp);
memset(&icmpv6_tmp, 0, sizeof icmpv6_tmp);
memset(&udpv6_tmp, 0, sizeof udpv6_tmp);
if ( (fp = fopen("net/snmp6", "r")) != NULL)
{
count_t countval;
int cur = 0;
/*
** one name-value pair per line
*/
while ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
nr = sscanf(linebuf, "%s %lld", nam, &countval);
if (nr < 2) /* unexpected line ? --> skip */
continue;
if (strcmp(v6tab[cur].nam, nam) == 0)
{
*(v6tab[cur].val) = countval;
}
else
{
for (cur=0; cur < v6tab_entries; cur++)
if (strcmp(v6tab[cur].nam, nam) == 0)
break;
if (cur < v6tab_entries) /* found ? */
*(v6tab[cur].val) = countval;
}
if (++cur >= v6tab_entries)
cur = 0;
}
memcpy(&si->net.ipv6, &ipv6_tmp, sizeof ipv6_tmp);
memcpy(&si->net.icmpv6, &icmpv6_tmp, sizeof icmpv6_tmp);
memcpy(&si->net.udpv6, &udpv6_tmp, sizeof udpv6_tmp);
fclose(fp);
}
/*
** check if extended partition-statistics are provided < kernel 2.6
*/
if ( part_stats && (fp = fopen("partitions", "r")) != NULL)
{
char diskname[256];
i = 0;
while ( fgets(linebuf, sizeof(linebuf), fp) )
{
nr = sscanf(linebuf,
"%*d %*d %*d %255s %lld %*d %lld %*d "
"%lld %*d %lld %*d %*d %lld %lld",
diskname,
&(si->xdsk.xdsk[i].nread),
&(si->xdsk.xdsk[i].nrsect),
&(si->xdsk.xdsk[i].nwrite),
&(si->xdsk.xdsk[i].nwsect),
&(si->xdsk.xdsk[i].io_ms),
&(si->xdsk.xdsk[i].avque) );
/*
** check if this line concerns the entire disk
** or just one of the partitions of a disk (to be
** skipped)
*/
if (nr == 7) /* full stats-line ? */
{
if ( !isrealdisk(diskname,
si->xdsk.xdsk[i].name,
MAXDKNAM) )
continue;
if (++i >= MAXDSK-1)
break;
}
}
si->xdsk.xdsk[i].name[0] = '\0'; /* set terminator for table */
si->xdsk.nrxdsk = i;
fclose(fp);
if (i == 0)
part_stats = 0; /* do not try again for next cycles */
}
/*
** check if disk-statistics are provided (kernel 2.6 onwards)
*/
if ( (fp = fopen("diskstats", "r")) != NULL)
{
char diskname[256];
i = 0;
while ( fgets(linebuf, sizeof(linebuf), fp) )
{
nr = sscanf(linebuf,
"%*d %*d %255s %lld %*d %lld %*d "
"%lld %*d %lld %*d %*d %lld %lld",
diskname,
&(si->xdsk.xdsk[i].nread),
&(si->xdsk.xdsk[i].nrsect),
&(si->xdsk.xdsk[i].nwrite),
&(si->xdsk.xdsk[i].nwsect),
&(si->xdsk.xdsk[i].io_ms),
&(si->xdsk.xdsk[i].avque) );
/*
** check if this line concerns the entire disk
** or just one of the partitions of a disk (to be
** skipped)
*/
if (nr == 7) /* full stats-line ? */
{
if ( !isrealdisk(diskname,
si->xdsk.xdsk[i].name,
MAXDKNAM) )
continue;
if (++i >= MAXDSK-1)
break;
}
}
si->xdsk.xdsk[i].name[0] = '\0'; /* set terminator for table */
si->xdsk.nrxdsk = i;
fclose(fp);
}
chdir(origdir);
/*
** fetch application-specific counters
*/
#if HTTPSTATS
if ( wwwvalid)
wwwvalid = getwwwstat(80, &(si->www));
#endif
}
/*
** set of subroutines to determine which disks should be monitored
** and to translate long name strings into short name strings
*/
static void
nullmodname(char *curname, char *newname, int maxlen)
{
strncpy(newname, curname, maxlen-1);
*(newname+maxlen-1) = 0;
}
static void
abbrevname1(char *curname, char *newname, int maxlen)
{
char cutype[128];
int hostnum, busnum, targetnum, lunnum;
sscanf(curname, "%[^/]/host%d/bus%d/target%d/lun%d",
cutype, &hostnum, &busnum, &targetnum, &lunnum);
snprintf(newname, maxlen, "%c-h%db%dt%d",
cutype[0], hostnum, busnum, targetnum);
}
/*
** table contains the names (in regexp format) of valid disks
** to be supported, together a function to modify the name-strings
** (i.e. to abbreviate long strings);
** this table is used in the function isrealdisk()
*/
static struct {
char *regexp;
regex_t compreg;
void (*modname)(char *, char *, int);
} validdisk[] = {
{ "^sd[a-z][a-z]*$", {0}, nullmodname, },
{ "^hd[a-z]$", {0}, nullmodname, },
{ "^rd/c[0-9][0-9]*d[0-9][0-9]*$", {0}, nullmodname, },
{ "^cciss/c[0-9][0-9]*d[0-9][0-9]*$", {0}, nullmodname, },
{ "/host.*/bus.*/target.*/lun.*/disc", {0}, abbrevname1, },
/* virtual disks */
{ "^xvd[a-z][a-z]*$", {0}, nullmodname, },
{ "^dasd[a-z][a-z]*$", {0}, nullmodname, },
};
static int
isrealdisk(char *curname, char *newname, int maxlen)
{
static int firstcall = 1;
register int i;
if (firstcall) /* compile the regular expressions */
{
for (i=0; i < sizeof validdisk/sizeof validdisk[0]; i++)
regcomp(&validdisk[i].compreg, validdisk[i].regexp,
REG_NOSUB);
firstcall = 0;
}
/*
** try to recognize one of the compiled regular expressions
*/
for (i=0; i < sizeof validdisk/sizeof validdisk[0]; i++)
{
if (regexec(&validdisk[i].compreg, curname, 0, NULL, 0) == 0)
{
/*
** name-string recognized; modify name-string
*/
(*validdisk[i].modname)(curname, newname, maxlen);
return 1;
}
}
return 0;
}
/*
** LINUX SPECIFIC:
** Determine boot-time of this system (as number of seconds since 1-1-1970).
*/
time_t getbootlinux(long);
time_t
getbootlinux(long hertz)
{
int cpid;
char tmpbuf[1280];
FILE *fp;
unsigned long startticks;
time_t boottime = 0;
/*
** dirty hack to get the boottime, since the
** Linux 2.6 kernel (2.6.5) does not return a proper
** boottime-value with the times() system call :-(
*/
if ( (cpid = fork()) == 0 )
{
/*
** child just waiting to be killed by parent
*/
pause();
}
else
{
/*
** parent determines start-time (in clock-ticks since boot)
** of the child and calculates the boottime in seconds
** since 1-1-1970
*/
snprintf(tmpbuf, sizeof tmpbuf, "/proc/%d/stat", cpid);
if ( (fp = fopen(tmpbuf, "r")) != NULL)
{
if ( fscanf(fp, "%*d (%*[^)]) %*c %*d %*d %*d %*d "
"%*d %*d %*d %*d %*d %*d %*d %*d "
"%*d %*d %*d %*d %*d %*d %lu",
&startticks) == 1)
{
boottime = time(0) - startticks / hertz;
}
fclose(fp);
}
/*
** kill the child and get rid of the zombie
*/
kill(cpid, SIGKILL);
(void) wait((int *)0);
}
return boottime;
}
#if HTTPSTATS
/*
** retrieve statistics from local HTTP daemons
** via http://localhost/server-status?auto
*/
int
getwwwstat(unsigned short port, struct wwwstat *wp)
{
int sockfd, tobefound;
FILE *sockfp;
struct sockaddr_in sockname;
char linebuf[4096];
char label[512];
long long value;
memset(wp, 0, sizeof *wp);
/*
** allocate a socket and connect to the local HTTP daemon
*/
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
return 0;
sockname.sin_family = AF_INET;
sockname.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sockname.sin_port = htons(port);
if ( connect(sockfd, (struct sockaddr *) &sockname,
sizeof sockname) == -1)
{
close(sockfd);
return 0;
}
/*
** write a GET-request for /server-status
*/
if ( write(sockfd, HTTPREQ, sizeof HTTPREQ) < sizeof HTTPREQ)
{
close(sockfd);
return 0;
}
/*
** remap socket descriptor to a stream to allow stdio calls
*/
sockfp = fdopen(sockfd, "r+");
/*
** read response line by line
*/
tobefound = 5; /* number of values to be searched */
while ( fgets(linebuf, sizeof linebuf, sockfp) && tobefound)
{
/*
** handle line containing status code
*/
if ( strncmp(linebuf, "HTTP/", 5) == 0)
{
sscanf(linebuf, "%511s %lld %*s\n", label, &value);
if (value != 200) /* HTTP-request okay? */
{
fclose(sockfp);
close(sockfd);
return 0;
}
continue;
}
/*
** decode line and search for the required counters
*/
if (sscanf(linebuf, "%511[^:]: %lld\n", label, &value) == 2)
{
if ( strcmp(label, "Total Accesses") == 0)
{
wp->accesses = value;
tobefound--;
}
if ( strcmp(label, "Total kBytes") == 0)
{
wp->totkbytes = value;
tobefound--;
}
if ( strcmp(label, "Uptime") == 0)
{
wp->uptime = value;
tobefound--;
}
if ( strcmp(label, "BusyWorkers") == 0)
{
wp->bworkers = value;
tobefound--;
}
if ( strcmp(label, "IdleWorkers") == 0)
{
wp->iworkers = value;
tobefound--;
}
}
}
fclose(sockfp);
close(sockfd);
return 1;
}
#endif
|