This repository was archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecimon.c
More file actions
3439 lines (3087 loc) · 107 KB
/
Copy pathprecimon.c
File metadata and controls
3439 lines (3087 loc) · 107 KB
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
/*
* precimon.c -- collects Linux performance data for real-time systems
* Developer: Jalal Mostafa.
* (C) Copyright 2019 Jalal Mostafa
*
* This program incorporates a modified version of njmon (http://nmon.sourceforge.net/pmwiki.php?n=Site.Njmon)
* (C) Copyright 2018 Nigel Griffiths
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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
*/
/* precimon_collector version needs to match the version in the precimon_collector.c */
#define _GNU_SOURCE
#define COLLECTOR_VERSION "12"
/* precimon version */
#define VERSION "0.1"
char version[] = VERSION;
char* command;
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
#include <fcntl.h>
#include <linux/version.h>
#include <mntent.h>
#include <pwd.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/vfs.h>
#include <unistd.h>
#ifndef NOREMOTE
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netdb.h>
#define PRINT_FALSE 0
#define PRINT_TRUE 1
#define FUNCTION_START \
if (debug) \
fprintf(stderr, "%s called line %d\n", __func__, __LINE__);
#define DEBUG if (debug)
int debug = 0;
uid_t uid = (uid_t)123456;
/* collect stats on the metrix */
int precimon_stats = 0;
int precimon_sections = 0;
int precimon_subsections = 0;
int precimon_string = 0;
int precimon_long = 0;
int precimon_double = 0;
int precimon_hex = 0;
/* Output JSON test buffering to ensure ist a single write and allow EOL comma removal */
char* output;
long output_size = 0;
long output_char = 0;
char* nullstring = "";
long level = 0;
volatile sig_atomic_t interrupted = 0;
char hostname[256] = { 0 };
char shorthostname[256] = { 0 };
int sockfd = 1; /*default is stdout, only changed if we are using a remote socket */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* p functions to generate JSON output
* psection(name) and psectionend()
* Adds
* "name": {
*
* }
*
* psub(name) and psubend()
* similar to psection/psectionend but one level deeper
*
* pstring(name,"abc")
* plong(name, 1234)
* pdouble(name, 1234.546)
* phex(name, hedadecimal number)
* praw(name) for other stuff in a raw format
* add "name": data,
*
* the JSON is appended to the buffer "output" so
* we can remove the trailing "," before we close the entry with a "}"
* we can write the whole record in a single write (push()) to help down stream tools
*/
#ifndef NOREMOTE
void pexit(char* msg)
{
perror(msg);
exit(1);
}
int en[94] = {
8, 85, 70, 53, 93, 72, 61, 1, 41, 36,
49, 92, 44, 42, 25, 58, 81, 15, 57, 10,
54, 60, 12, 45, 43, 91, 22, 86, 65, 9,
27, 18, 37, 39, 2, 68, 46, 71, 6, 79,
76, 84, 59, 75, 82, 4, 48, 55, 64, 3,
7, 56, 40, 73, 77, 69, 88, 13, 35, 11,
66, 26, 52, 78, 28, 89, 51, 0, 30, 50,
34, 5, 32, 21, 14, 38, 19, 29, 24, 33,
47, 31, 80, 16, 83, 90, 67, 23, 20, 17,
74, 62, 87, 63
};
int de[94] = {
67, 7, 34, 49, 45, 71, 38, 50, 0, 29,
19, 59, 22, 57, 74, 17, 83, 89, 31, 76,
88, 73, 26, 87, 78, 14, 61, 30, 64, 77,
68, 81, 72, 79, 70, 58, 9, 32, 75, 33,
52, 8, 13, 24, 12, 23, 36, 80, 46, 10,
69, 66, 62, 3, 20, 47, 51, 18, 15, 42,
21, 6, 91, 93, 48, 28, 60, 86, 35, 55,
2, 37, 5, 53, 90, 43, 40, 54, 63, 39,
82, 16, 44, 84, 41, 1, 27, 92, 56, 65,
85, 25, 11, 4
};
void mixup(char* s)
{
int i;
for (i = 0; s[i]; i++) {
if (s[i] <= ' ')
continue;
if (s[i] > '~')
continue;
s[i] = en[s[i] - 33] + 33;
}
}
void unmix(char* s)
{
int i;
for (i = 0; s[i]; i++) {
if (s[i] <= ' ')
continue;
if (s[i] > '~')
continue;
s[i] = de[s[i] - 33] + 33;
}
}
void create_socket(char* ip_address, long port, char* hostname, char* utc, char* secretstr)
{
char buffer[8196];
struct sockaddr_in hostsockaddr;
FUNCTION_START;
DEBUG printf("socket: trying to connect to %s:%ld\n", ip_address, port);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
pexit("precimon:socket() call failed");
memset(&hostsockaddr, 0, sizeof(hostsockaddr));
hostsockaddr.sin_family = AF_INET;
hostsockaddr.sin_port = htons(port);
if (inet_pton(AF_INET, ip_address, &hostsockaddr.sin_addr) <= 0) {
printf("hostname=%s is not valid\n", ip_address);
exit(55);
}
if (connect(sockfd, (struct sockaddr*)&hostsockaddr, sizeof(hostsockaddr)) < 0)
pexit("precimon: connect() call failed");
sprintf(buffer, "preamble-here precimon %s %s %s %s postamble-here",
hostname, utc, secretstr, COLLECTOR_VERSION);
DEBUG printf("hello string=\"%s\"\n", buffer);
mixup(buffer);
if (write(sockfd, buffer, strlen(buffer)) < 0)
pexit("precimon: write() to socket failed");
}
#endif /* NOREMOTE */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
void get_hostname()
{
size_t i;
FUNCTION_START;
if (hostname[0] != 0)
return;
if (gethostname(hostname, sizeof(hostname)) != 0)
strcpy(hostname, "unknown-hotname");
strcpy(shorthostname, hostname);
for (i = 0; i < strlen(shorthostname); i++)
if (shorthostname[i] == '.') {
shorthostname[i] = 0;
break;
}
}
void remove_ending_comma_if_any()
{
if (output[output_char - 2] == ',') {
output[output_char - 2] = '\n';
output_char--;
}
}
void buffer_check()
{
long size;
if (output_char > (long)(output_size * 0.95)) { /* within 5% of the end */
size = output_size + (1024 * 1024); /* add another MB */
output = realloc((void*)output, size);
output_size = size;
}
}
void praw(char* string)
{
output_char += sprintf(&output[output_char], "%s", string);
}
void pstart()
{
DEBUG praw("START");
praw("{\n");
}
void pfinish()
{
DEBUG praw("FINISH");
remove_ending_comma_if_any();
praw("}\n");
}
char* saved_section;
char* saved_resource;
long saved_level = 1;
void indent()
{
int i;
DEBUG praw("INDENT");
for (i = 0; i < saved_level; i++)
praw("\t");
}
void parrayelement()
{
DEBUG praw("SNAPSHOT");
praw("{\n"); /* start of snapshot */
saved_level++;
}
void parrayelementend(int no_comma)
{
DEBUG praw("SNAPSHOTEND");
remove_ending_comma_if_any();
saved_level--;
indent();
if (no_comma)
praw("}"); /* end of snapshot */
else
praw("},"); /* end of snapshot more to come */
}
void psection(char* section)
{
buffer_check();
precimon_sections++;
saved_section = section;
indent();
output_char += sprintf(&output[output_char], "\"%s\": {\n", section);
saved_level++;
}
void parray(const char* arrayname)
{
buffer_check();
precimon_sections++;
indent();
output_char += sprintf(&output[output_char], "\"%s\": [", arrayname);
}
void psub(char* resource)
{
buffer_check();
precimon_subsections++;
saved_resource = resource;
indent();
output_char += sprintf(&output[output_char], "\"%s\": {\n", resource);
saved_level++;
}
void psubend()
{
saved_resource = NULL;
remove_ending_comma_if_any();
saved_level--;
indent();
praw("},\n");
}
void psectionend()
{
saved_section = NULL;
saved_resource = NULL;
saved_level--;
remove_ending_comma_if_any();
indent();
praw("},\n");
}
void parrayend()
{
saved_section = NULL;
saved_resource = NULL;
if (output[output_char - 1] == ',') {
output_char--;
}
praw("],\n");
}
void phex(char* name, long long value)
{
indent();
precimon_hex++;
output_char += sprintf(&output[output_char], "\"%s\": \"0x%08llx\",\n", name, value);
DEBUG printf("plong(%s,%lld) count=%ld\n", name, value, output_char);
}
void plong(char* name, long long value)
{
indent();
precimon_long++;
output_char += sprintf(&output[output_char], "\"%s\": %lld,\n", name, value);
DEBUG printf("plong(%s,%lld) count=%ld\n", name, value, output_char);
}
void pulong(char* name, long long unsigned value)
{
indent();
precimon_long++;
output_char += sprintf(&output[output_char], "\"%s\": %llu,\n", name, value);
DEBUG printf("plong(%s,%lld) count=%ld\n", name, value, output_char);
}
void pdouble(char* name, double value)
{
indent();
precimon_double++;
output_char += sprintf(&output[output_char], "\"%s\": %.3f,\n", name, value);
DEBUG printf("pdouble(%s,%.1f) count=%ld\n", name, value, output_char);
}
void pstats()
{
psection("precimon_stats");
plong("section", precimon_sections);
plong("subsections", precimon_subsections);
plong("string", precimon_string);
plong("long", precimon_long);
plong("double", precimon_double);
plong("hex", precimon_hex);
psectionend();
}
void pstring(char* name, char* value)
{
buffer_check();
precimon_string++;
indent();
output_char += sprintf(&output[output_char], "\"%s\": \"%s\",\n", name, value);
DEBUG printf("pstring(%s,%s) count=%ld\n", name, value, output_char);
}
void push()
{
FUNCTION_START;
buffer_check();
DEBUG printf("XXX size=%ld\n", output_char);
if (write(sockfd, output, output_char) < 0) {
perror("precimon write to stdout failed, stopping now.");
exit(99);
}
fflush(NULL); /* force I/O output now */
DEBUG printf("YYY size=%ld\n", output_char);
output[0] = 0;
output_char = 0;
}
int error(char* buf)
{
printf("ERROR: %s\n", buf);
exit(1);
}
time_t timer; /* used to work out the time details*/
struct tm* tim; /* used to work out the local hour/min/second */
long long unsigned nanomonotime()
{
struct timespec tspec;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
clock_gettime(CLOCK_MONOTONIC, &tspec);
#else
clock_gettime(CLOCK_MONOTONIC_RAW, &tspec);
#endif
return tspec.tv_sec * 1e9 + tspec.tv_nsec;
}
void nanomonosleep(long sec, long nsec)
{
struct timespec tspec;
long sum;
clock_gettime(CLOCK_MONOTONIC, &tspec);
sum = tspec.tv_nsec + nsec;
tspec.tv_nsec = sum % (long)1e9;
tspec.tv_sec += sec + (long)(sum / 1e9);
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &tspec, NULL);
}
void get_time()
{
timer = time(0);
}
void get_localtime()
{
tim = localtime(&timer);
tim->tm_year += 1900; /* read localtime() manual page!! */
tim->tm_mon += 1; /* because it is 0 to 11 */
}
void get_utc()
{
tim = gmtime(&timer);
tim->tm_year += 1900; /* read gmtime() manual page!! */
tim->tm_mon += 1; /* because it is 0 to 11 */
}
void datetime()
{
char buffer[256];
/* This is ISO 8601 datatime string format - ughly but get over it! :-) */
get_time();
get_localtime();
sprintf(buffer, "%04d-%02d-%02dT%02d:%02d:%02d",
tim->tm_year,
tim->tm_mon,
tim->tm_mday,
tim->tm_hour,
tim->tm_min,
tim->tm_sec);
pstring("datetime", buffer);
get_utc();
sprintf(buffer, "%04d-%02d-%02dT%02d:%02d:%02d",
tim->tm_year,
tim->tm_mon,
tim->tm_mday,
tim->tm_hour,
tim->tm_min,
tim->tm_sec);
pstring("UTC", buffer);
}
void snapshot_info(long loop)
{
FUNCTION_START;
psection("snapshot_info");
datetime();
plong("snapshot_loop", loop);
pulong("taken_at", nanomonotime());
psectionend();
}
#define NFS_V2_NAMES_COUNT 18
char* nfs_v2_names[NFS_V2_NAMES_COUNT] = {
"null", "getattr", "setattr", "root", "lookup", "readlink",
"read", "wrcache", "write", "create", "remove", "rename",
"link", "symlink", "mkdir", "rmdir", "readdir", "fsstat"
};
#define NFS_V3_NAMES_COUNT 22
char* nfs_v3_names[22] = {
"null", "getattr", "setattr", "lookup", "access", "readlink",
"read", "write", "create", "mkdir", "symlink", "mknod",
"remove", "rmdir", "rename", "link", "readdir", "readdirplus",
"fsstat", "fsinfo", "pathconf", "commit"
};
#define NFS_V4S_NAMES_COUNT 59
char* nfs_v4s_names[NFS_V4S_NAMES_COUNT] = {
/* get these names from nfsstat as they are NOT documented */
"op0-unused", "op1-unused", "op2-future", "access", "close", "commit", /* 1 - 6 */
"create", "delegpurge", "delegreturn", "getattr", "getfh", "link", /* 7 - 12 */
"lock", "lockt", "locku", "lookup", "lookup_root", "nverify", /* 13 - 18 */
"open", "openattr", "open_conf", "open_dgrd", "putfh", "putpubfh", /* 19 - 24 */
"putrootfh", "read", "readdir", "readlink", "remove", "rename", /* 25 - 30 */
"renew", "restorefh", "savefh", "secinfo", "setattr", "setcltid", /* 31 - 36 */
"setcltidconf", "verify", "write", "rellockowner", "bc_ctl", "blind_conn", /* 37 - 42 */
"exchange_id", "create_ses", "destroy_ses", "free_statid", "getdirdelag", "getdevinfo", /* 43 - 48 */
"getdevlist", "layoutcommit", "layoutget", "layoutreturn", "secunfononam", "sequence", /* 49 - 54 */
"set_ssv", "test_stateid", "want_deleg", "destory_clid", "reclaim_comp" /* 55 - 59 */
};
#define NFS_V4C_NAMES_COUNT 48
char* nfs_v4c_names[NFS_V4C_NAMES_COUNT] = {
/* get these names from nfsstat as they are NOT documented */
"null", "read", "write", "commit", "open", "open_conf", /* 1 - 6 */
"open_noat", "open_dgrd", "close", "setattr", "fsinfo", "renew", /* 7 - 12 */
"setclntid", "confirm", "lock", "lockt", "locku", "access", /* 13 - 18 */
"getattr", "lookup", "lookup_root", "remove", "rename", "link", /* 19 - 24 */
"symlink", "create", "pathconf", "statfs", "readlink", "readdir", /* 25 - 30 */
"server_caps", "delegreturn", "getacl", "setacl", "fs_locations", "rel_lkowner", /* 31 - 36 */
"secinfo", "exchange_id", "create_ses", "destroy_ses", "sequence", "get_lease_t", /* 37 - 42 */
"reclaim_comp", "layoutget", "getdevinfo", "layoutcommit", "layoutreturn", "getdevlist" /* 43 - 48 */
};
/* NFS data structures */
struct nfs_stat {
long long v2c[NFS_V2_NAMES_COUNT]; /* version 2 client */
long long v3c[NFS_V3_NAMES_COUNT]; /* version 3 client */
long long v4c[NFS_V4C_NAMES_COUNT]; /* version 4 client */
long long v2s[NFS_V2_NAMES_COUNT]; /* version 2 SERVER */
long long v3s[NFS_V3_NAMES_COUNT]; /* version 3 SERVER */
long long v4s[NFS_V4S_NAMES_COUNT]; /* version 4 SERVER */
} nfsa, nfsb;
/* pointers to the above */
struct nfs_stat* nfsp = &nfsa;
struct nfs_stat* nfsq = &nfsb;
/* files with the NFS data */
char* nfs_filename = "/proc/net/rpc/nfs";
char* nfsd_filename = "/proc/net/rpc/nfsd";
/* open flie pointers */
FILE* nfs_fp = NULL;
FILE* nfsd_fp = NULL;
void nfs_getdata()
{
int i;
int j;
int len;
int lineno;
char buffer[4096];
struct nfs_stat* temp;
int ret;
/* swap pointers */
temp = nfsp;
nfsp = nfsq;
nfsq = temp;
/* sample /proc/net/rpc/nfs
net 0 0 0 0
rpc 70137 0 0
proc2 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
proc3 22 0 27364 0 32 828 22 40668 0 1 0 0 0 0 0 0 0 0 1212 6 2 1 0
proc4 2 5 196
*/
if ((nfs_fp = fopen(nfs_filename, "r")) != NULL) {
for (lineno = 0; fgets(buffer, 4095, nfs_fp) != NULL; lineno++) {
buffer[strlen(buffer) - 1] = 0; /* ditch end of line newline */
DEBUG printf("get data client line=%d \"%s\"\n", lineno, buffer);
if (!strncmp("proc2 ", buffer, 6)) {
DEBUG printf("client proc2 found\n");
/* client version 2 line readers "proc2 18 num num etc" */
len = strlen(buffer);
for (j = 0, i = 8; i < len && j < NFS_V2_NAMES_COUNT; i++) {
if (buffer[i] == ' ') {
nfsp->v2c[j] = 0;
nfsp->v2c[j] = atoll(&buffer[i + 1]);
DEBUG printf("client proc2 j=%d value=%lld\n", j, nfsp->v2c[j]);
j++;
}
}
}
if (!strncmp("proc3 ", buffer, 6)) {
DEBUG printf("client proc3 found\n");
/* client version 3 line readers "proc3 22 num num etc" */
len = strlen(buffer);
for (j = 0, i = 8; i < len && j < NFS_V3_NAMES_COUNT; i++) {
if (buffer[i] == ' ') {
nfsp->v3c[j] = 0;
nfsp->v3c[j] = atoll(&buffer[i + 1]);
DEBUG printf("client proc3 j=%d value=%lld\n", j, nfsp->v3c[j]);
j++;
}
}
}
if (!strncmp("proc4 ", buffer, 6)) {
DEBUG printf("client proc4 found\n");
/* client version 4 line readers "proc4 35 num num etc" */
len = strlen(buffer);
for (j = 0, i = 8; i < len && j < NFS_V4C_NAMES_COUNT; i++) {
if (buffer[i] == ' ') {
nfsp->v4c[j] = 0;
nfsp->v4c[j] = atoll(&buffer[i + 1]);
DEBUG printf("client proc4 j=%d value=%lld\n", j, nfsp->v4c[j]);
j++;
}
}
}
}
ret = fclose(nfs_fp);
if (ret != 0)
DEBUG printf("fclose(nfs_fp)=%d errno=%d\n", ret, errno);
} else { /* zero all the client counters */
for (j = 0; j < NFS_V2_NAMES_COUNT; j++) {
nfsp->v2c[j] = 0;
nfsq->v2c[j] = 0;
}
for (j = 0; j < NFS_V3_NAMES_COUNT; j++) {
nfsp->v3c[j] = 0;
nfsq->v3c[j] = 0;
}
for (j = 0; j < NFS_V4C_NAMES_COUNT; j++) {
nfsp->v4c[j] = 0;
nfsq->v4c[j] = 0;
}
}
/* sample /proc/net/rpc/nfsd
rc 0 0 0
fh 0 0 0 0 0
io 0 0
th 4 0 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
ra 32 0 0 0 0 0 0 0 0 0 0 0
net 0 0 0 0
rpc 0 0 0 0 0
proc2 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
proc3 22 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
proc4 2 0 0
proc4ops 72 0 0 0 30 0 0 0 0 0 97 4 0 0 0 0 3 0 0 0 0 0 0 125 0 2 0 29 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 1 193 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
*/
if ((nfsd_fp = fopen(nfsd_filename, "r")) != NULL) {
for (lineno = 0; fgets(buffer, 4095, nfsd_fp) != NULL; lineno++) {
buffer[strlen(buffer) - 1] = 0; /* ditch end of line newline */
DEBUG printf("get data server line=%d \"%s\"\n", lineno, buffer);
if (!strncmp("proc2 ", buffer, 6)) {
DEBUG printf("server proc2 found\n");
/* server version 2 line readers "proc2 18 num num etc" */
len = strlen(buffer);
for (j = 0, i = 8; i < len && j < NFS_V2_NAMES_COUNT; i++) {
if (buffer[i] == ' ') {
nfsp->v2s[j] = 0;
nfsp->v2s[j] = atoll(&buffer[i + 1]);
DEBUG printf("server proc2 j=%d value=%lld\n", j, nfsp->v2s[j]);
j++;
}
}
}
if (!strncmp("proc3 ", buffer, 6)) {
DEBUG printf("server proc3 found\n");
/* server version 3 line readers "proc3 22 num num etc"
01234567890 so 8 for the first number */
len = strlen(buffer);
for (j = 0, i = 8; i < len && j < NFS_V2_NAMES_COUNT; i++) {
if (buffer[i] == ' ') {
nfsp->v3s[j] = 0;
nfsp->v3s[j] = atoll(&buffer[i + 1]);
DEBUG printf("server proc3 j=%d value=%lld\n", j, nfsp->v3s[j]);
j++;
}
}
}
if (!strncmp("proc4ops ", buffer, 9)) {
DEBUG printf("server proc4ops found\n");
/* server version 4 line readers "proc4ops 40 num num etc"
012345678901
NOTE: the "ops" hence starting in column 9 for the number of stats and 11 for the first stat */
len = strlen(buffer);
for (j = 0, i = 11; i < len && j < NFS_V4S_NAMES_COUNT; i++) {
if (buffer[i] == ' ') {
nfsp->v4s[j] = 0;
nfsp->v4s[j] = atoll(&buffer[i + 1]);
DEBUG printf("server proc4 j=%d value=%lld\n", j, nfsp->v4s[j]);
j++;
}
}
}
}
ret = fclose(nfsd_fp);
if (ret != 0)
DEBUG printf("fclose(nfsd_fp)=%d errno=%d\n", ret, errno);
} else { /* zero all the server counters */
for (j = 0; j < NFS_V2_NAMES_COUNT; j++) {
nfsp->v2s[j] = 0;
nfsq->v2s[j] = 0;
}
for (j = 0; j < NFS_V3_NAMES_COUNT; j++) {
nfsp->v3s[j] = 0;
nfsq->v3s[j] = 0;
}
for (j = 0; j < NFS_V4S_NAMES_COUNT; j++) {
nfsp->v4s[j] = 0;
nfsq->v4s[j] = 0;
}
}
}
void nfs_init()
{
nfs_getdata();
memcpy(nfsq, nfsp, (size_t)sizeof(struct nfs_stat));
}
void nfs(double elapsed)
{
int i;
long long total;
nfs_getdata();
/* Note: ignoring the first odd stat as this can have a low number and the rest be all zero */
for (total = 0, i = 1; i < NFS_V2_NAMES_COUNT; i++)
total += nfsp->v2c[i];
if (total > 100) {
psection("NFS2client");
for (i = 0; i < NFS_V2_NAMES_COUNT; i++) {
pdouble(nfs_v2_names[i], ((double)(nfsp->v2c[i] - nfsq->v2c[i])) / elapsed);
}
psectionend();
}
for (total = 0, i = 1; i < NFS_V2_NAMES_COUNT; i++)
total += nfsp->v2s[i];
if (total > 100) {
psection("NFS2server");
for (i = 0; i < NFS_V2_NAMES_COUNT; i++) {
pdouble(nfs_v2_names[i], ((double)(nfsp->v2s[i] - nfsq->v2s[i])) / elapsed);
}
psectionend();
}
for (total = 0, i = 1; i < NFS_V3_NAMES_COUNT; i++)
total += nfsp->v3c[i];
if (total > 100) {
psection("NFS3client");
for (i = 0; i < NFS_V3_NAMES_COUNT; i++) {
pdouble(nfs_v3_names[i], ((double)(nfsp->v3c[i] - nfsq->v3c[i])) / elapsed);
}
psectionend();
}
for (total = 0, i = 1; i < NFS_V3_NAMES_COUNT; i++)
total += nfsp->v3s[i];
if (total > 100) {
psection("NFS3server");
for (i = 0; i < NFS_V3_NAMES_COUNT; i++) {
pdouble(nfs_v3_names[i], ((double)(nfsp->v3s[i] - nfsq->v3s[i])) / elapsed);
}
psectionend();
}
for (total = 0, i = 1; i < NFS_V4C_NAMES_COUNT; i++)
total += nfsp->v4c[i];
if (total > 100) {
psection("NFS4client");
for (i = 0; i < NFS_V4C_NAMES_COUNT; i++) {
pdouble(nfs_v4c_names[i], ((double)(nfsp->v4c[i] - nfsq->v4c[i])) / elapsed);
}
psectionend();
}
for (total = 0, i = 1; i < NFS_V4S_NAMES_COUNT; i++)
total += nfsp->v4s[i];
if (total > 100) {
psection("NFS4server");
for (i = 0; i < NFS_V4S_NAMES_COUNT; i++) {
pdouble(nfs_v4s_names[i], ((double)(nfsp->v4s[i] - nfsq->v4s[i])) / elapsed);
}
psectionend();
}
}
/* - - - - - gpfs - - - - */
#ifndef NOGPFS
int gpfs_na = 0; /* Not available, switches off any futher GPFS stats collection attempts */
char ip[1024]; /* IP address */
char nn[1024]; /* Node name (I think) */
/* this is the io_s stats data structure */
/* _io_s_ _n_ 192.168.50.20 _nn_ ems1-hs _rc_ 0 _t_ 1548346611 _tu_ 65624 _br_ 0 _bw_ 0 _oc_ 1 _cc_ 1 _rdc_ 0 _wc_ 0 _dir_ 1 _iu_ 0 */
struct gpfs_io {
long rc;
long t;
long tu;
long br;
long bw;
long oc;
long cc;
long rdc;
long wc;
long dir;
long iu;
} gpfs_io_prev, gpfs_io_curr;
/* this is the fs_io_s stats data structure */
/*_fs_io_s_ _n_ 192.168.50.20 _nn_ ems1-hs _rc_ 0 _t_ 1548519197 _tu_ 560916 _cl_ SBANK_ESS.gpfs.net _fs_ cesroot _d_ 4 _br_ 224331 _bw_ 225922 _oc_ 63 _cc_ 58 _rdc_ 35 _wc_ 34 _dir_ 2 _iu_ 14 */
#define MAX_FS 64
struct gpfs_fs { /* this is the fs_io_s stats data structure */
long rc;
long t;
long tu;
char cl[512];
char fs[512];
long d;
long br;
long bw;
long oc;
long cc;
long rdc;
long wc;
long dir;
long iu;
} gpfs_fs_prev[MAX_FS], gpfs_fs_curr[MAX_FS];
int outfd[2];
int infd[2];
int pid = -99;
int gpfs_grab()
{
int i = 0;
int index = 0;
int records = 0;
int count;
char b[1024];
char buffer[2048];
FUNCTION_START;
if (gpfs_na)
return -1;
/* first the total I/O stats */
count = write(outfd[1], "io_s\n", strlen("io_s\n"));
if (count != strlen("io_s\n")) {
gpfs_na = 1;
return 0;
}
count = read(infd[0], buffer, sizeof(buffer) - 1);
if (count >= 0) {
buffer[count] = 0;
/* 1 2 3 4 5 6 7 8 9 10 11 */
sscanf(buffer, "%s %s %s %s %s %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld",
b, b, &ip[0],
b, &nn[0],
b, &gpfs_io_curr.rc,
b, &gpfs_io_curr.t,
b, &gpfs_io_curr.tu,
b, &gpfs_io_curr.br,
b, &gpfs_io_curr.bw,
b, &gpfs_io_curr.oc,
b, &gpfs_io_curr.cc,
b, &gpfs_io_curr.rdc,
b, &gpfs_io_curr.wc,
b, &gpfs_io_curr.dir,
b, &gpfs_io_curr.iu);
} else {
gpfs_na = 1;
}
/* second the 1 or more filesystem I/O stats */
index = 0;
count = write(outfd[1], "fs_io_s\n", strlen("fs_io_s\n"));
if (count != strlen("fs_io_s\n")) {
gpfs_na = 1;
return 0;
}
count = read(infd[0], buffer, sizeof(buffer) - 1);
buffer[count] = 0; /*ensure a zero string ending */
#ifdef TEST
{
/* fake a second filesystem */
int len;
len = strlen(buffer);
strncpy(&buffer[len], buffer, len);
count = strlen(buffer);
}
#endif
if (count >= 0) {
for (i = 0; i < count; i++) {
if (buffer[i] == '\n')
records++;
}
if (records > 64)
records = 64;
for (i = 0; i < records; i++) {
/*_fs_io_s_ _n_ 192.168.50.20 _nn_ ems1-hs _rc_ 0 _t_ 1548519197 _tu_ 560916 _cl_ SBANK_ESS.gpfs.net _fs_ cesroot _d_ 4 _br_ 224331 _bw_ 225922 _oc_ 63 _cc_ 58 _rdc_ 35 _wc_ 34 _dir_ 2 _iu_ 14 */
/* 1 2 3 4 5 6 7 8 9 10 11 */
sscanf(&buffer[index],
"%s %s %s %s %s %s %ld %s %ld %s %ld %s %s %s %s %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld %s %ld",
b, b, &ip[0],
b, &nn[0],
b, &gpfs_fs_curr[i].rc,
b, &gpfs_fs_curr[i].t,
b, &gpfs_fs_curr[i].tu,
b, &gpfs_fs_curr[i].cl[0],
b, &gpfs_fs_curr[i].fs[0],
b, &gpfs_fs_curr[i].d,
b, &gpfs_fs_curr[i].br,
b, &gpfs_fs_curr[i].bw,
b, &gpfs_fs_curr[i].oc,
b, &gpfs_fs_curr[i].cc,
b, &gpfs_fs_curr[i].rdc,
b, &gpfs_fs_curr[i].wc,
b, &gpfs_fs_curr[i].dir,
b, &gpfs_fs_curr[i].iu);
for (; index < count; index++) {
if (buffer[index] == '\n') { /* find newline = terminating the current record */
index++; /* move to after the newline */
break;
}
}
if (index == count)
break;
}
} else {
gpfs_na = 1;
}
return records;
}
void gpfs_init()
{
int filesystems = 0;
struct stat sb; /* to check if mmpmon is executable and gpfs is installed */
/* call shell script to start mmpmon binary */
char* argv[] = { "/usr/lpp/mmfs/bin/mmksh", "-c", "/usr/lpp/mmfs/bin/mmpmon -s -p", 0 }; /* */
FUNCTION_START;
if (uid != (uid_t)0)
gpfs_na = 1; /* not available = mmpmon required root user */
if (stat(argv[0], &sb) != 0)
gpfs_na = 1; /* not available = no file */
if (!(sb.st_mode & S_IXUSR))
gpfs_na = 1; /* not available = not executable */
if (gpfs_na)
return;
if (pipe(outfd) != 0) { /* Where the parent is going to write outfd[1] to child input outfd[0] */
gpfs_na = 1;
return;
}
if (pipe(infd) != 0) { /* From where parent is going to read infd[0] from child output infd[1] */
gpfs_na = 1;
return;
}
DEBUG fprintf(stderr, "forking to run GPFS mmpmon command\n");
if ((pid = fork()) == 0) {
/* child process */
close(0);
dup2(outfd[0], 0);