]> granicus.if.org Git - sysstat/blob - json_stats.c
sadf: Don't use an array for tot_jiffies variable
[sysstat] / json_stats.c
1 /*
2  * json_stats.c: Funtions used by sadf to display statistics in JSON format.
3  * (C) 1999-2017 by Sebastien GODARD (sysstat <at> orange.fr)
4  *
5  ***************************************************************************
6  * This program is free software; you can redistribute it and/or modify it *
7  * under the terms of the GNU General Public License as published  by  the *
8  * Free Software Foundation; either version 2 of the License, or (at  your *
9  * option) any later version.                                              *
10  *                                                                         *
11  * This program is distributed in the hope that it  will  be  useful,  but *
12  * WITHOUT ANY WARRANTY; without the implied warranty  of  MERCHANTABILITY *
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
14  * for more details.                                                       *
15  *                                                                         *
16  * You should have received a copy of the GNU General Public License along *
17  * with this program; if not, write to the Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA              *
19  ***************************************************************************
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
25
26 #include "sa.h"
27 #include "sadf.h"
28 #include "ioconf.h"
29 #include "json_stats.h"
30
31 #ifdef USE_NLS
32 #include <locale.h>
33 #include <libintl.h>
34 #define _(string) gettext(string)
35 #else
36 #define _(string) (string)
37 #endif
38
39 extern unsigned int flags;
40 extern unsigned int dm_major;
41
42 /*
43  ***************************************************************************
44  * Open or close "network" markup.
45  *
46  * IN:
47  * @tab         Number of tabulations.
48  * @action      Open or close action.
49  ***************************************************************************
50  */
51 void json_markup_network(int tab, int action)
52 {
53         static int markup_state = CLOSE_JSON_MARKUP;
54
55         if (action == markup_state)
56                 return;
57         markup_state = action;
58
59         if (action == OPEN_JSON_MARKUP) {
60                 /* Open markup */
61                 xprintf(tab, "\"network\": {");
62         }
63         else {
64                 /* Close markup */
65                 printf("\n");
66                 xprintf0(tab, "}");
67         }
68 }
69
70 /*
71  ***************************************************************************
72  * Open or close "power-management" markup.
73  *
74  * IN:
75  * @tab         Number of tabulations.
76  * @action      Open or close action.
77  ***************************************************************************
78  */
79 void json_markup_power_management(int tab, int action)
80 {
81         static int markup_state = CLOSE_JSON_MARKUP;
82
83         if (action == markup_state)
84                 return;
85         markup_state = action;
86
87         if (action == OPEN_JSON_MARKUP) {
88                 /* Open markup */
89                 xprintf(tab, "\"power-management\": {");
90         }
91         else {
92                 /* Close markup */
93                 printf("\n");
94                 xprintf0(tab, "}");
95         }
96 }
97
98 /*
99  ***************************************************************************
100  * Display CPU statistics in JSON.
101  *
102  * IN:
103  * @a           Activity structure with statistics.
104  * @curr        Index in array for current sample statistics.
105  * @tab         Indentation in output.
106  * @itv         Interval of time in 1/100th of a second (independent of the
107  *              number of processors). Unused here.
108  ***************************************************************************
109  */
110 __print_funct_t json_print_cpu_stats(struct activity *a, int curr, int tab,
111                                      unsigned long long itv)
112 {
113         int i, cpu_offline;
114         int sep = FALSE;
115         unsigned long long tot_jiffies_c, tot_jiffies_p;
116         unsigned long long deltot_jiffies;
117         struct stats_cpu *scc, *scp;
118         char cpuno[8];
119
120         xprintf(tab++, "\"cpu-load\": [");
121
122         for (i = 0; (i < a->nr) && (i < a->bitmap->b_size + 1); i++) {
123
124                 scc = (struct stats_cpu *) ((char *) a->buf[curr]  + i * a->msize);
125                 scp = (struct stats_cpu *) ((char *) a->buf[!curr] + i * a->msize);
126
127                 /* Should current CPU (including CPU "all") be displayed? */
128                 if (!(a->bitmap->b_array[i >> 3] & (1 << (i & 0x07))))
129                         /* No */
130                         continue;
131
132                 /*
133                  * Yes: Compute the total number of jiffies spent by current processor.
134                  * NB: Don't add cpu_guest/cpu_guest_nice because cpu_user/cpu_nice
135                  * already include them.
136                  */
137                 tot_jiffies_c = scc->cpu_user + scc->cpu_nice +
138                                 scc->cpu_sys + scc->cpu_idle +
139                                 scc->cpu_iowait + scc->cpu_hardirq +
140                                 scc->cpu_steal + scc->cpu_softirq;
141                 tot_jiffies_p = scp->cpu_user + scp->cpu_nice +
142                                 scp->cpu_sys + scp->cpu_idle +
143                                 scp->cpu_iowait + scp->cpu_hardirq +
144                                 scp->cpu_steal + scp->cpu_softirq;
145
146                 /* Total number of jiffies spent on the interval */
147                 deltot_jiffies = get_interval(tot_jiffies_p, tot_jiffies_c);
148
149                 if (sep) {
150                         printf(",\n");
151                 }
152                 sep = TRUE;
153
154                 if (!i) {
155                         /* This is CPU "all" */
156                         strcpy(cpuno, "all");
157                 }
158                 else {
159                         sprintf(cpuno, "%d", i - 1);
160
161                         /*
162                          * If the CPU is offline then it is omited from /proc/stat:
163                          * All the fields couldn't have been read and the sum of them is zero.
164                          * (Remember that guest/guest_nice times are already included in
165                          * user/nice modes.)
166                          */
167                         if (tot_jiffies_c == 0) {
168                                 /*
169                                  * Set current struct fields (which have been set to zero)
170                                  * to values from previous iteration. Hence their values won't
171                                  * jump from zero when the CPU comes back online.
172                                  */
173                                 *scc = *scp;
174
175                                 deltot_jiffies = 0;
176                                 cpu_offline = TRUE;
177                         }
178                         else {
179                                 /*
180                                  * Recalculate interval for current proc.
181                                  * If result is 0 then current CPU is a tickless one.
182                                  */
183                                 deltot_jiffies = get_per_cpu_interval(scc, scp);
184                                 cpu_offline = FALSE;
185                         }
186
187                         if (!deltot_jiffies) {
188                                 /* Current CPU is offline or tickless */
189                                 if (DISPLAY_CPU_DEF(a->opt_flags)) {
190                                         xprintf0(tab, "{\"cpu\": \"%d\", "
191                                                  "\"user\": %.2f, "
192                                                  "\"nice\": %.2f, "
193                                                  "\"system\": %.2f, "
194                                                  "\"iowait\": %.2f, "
195                                                  "\"steal\": %.2f, "
196                                                  "\"idle\": %.2f}",
197                                                  i - 1, 0.0, 0.0, 0.0, 0.0, 0.0,
198                                                  cpu_offline ? 0.0 : 100.0);
199                                 }
200                                 else if (DISPLAY_CPU_ALL(a->opt_flags)) {
201                                         xprintf0(tab, "{\"cpu\": \"%d\", "
202                                                  "\"usr\": %.2f, "
203                                                  "\"nice\": %.2f, "
204                                                  "\"sys\": %.2f, "
205                                                  "\"iowait\": %.2f, "
206                                                  "\"steal\": %.2f, "
207                                                  "\"irq\": %.2f, "
208                                                  "\"soft\": %.2f, "
209                                                  "\"guest\": %.2f, "
210                                                  "\"gnice\": %.2f, "
211                                                  "\"idle\": %.2f}",
212                                                  i - 1, 0.0, 0.0, 0.0, 0.0,
213                                                  0.0, 0.0, 0.0, 0.0, 0.0,
214                                                  cpu_offline ? 0.0 : 100.0);
215                                 }
216                                 continue;
217                         }
218                 }
219
220                 if (DISPLAY_CPU_DEF(a->opt_flags)) {
221                         xprintf0(tab, "{\"cpu\": \"%s\", "
222                                  "\"user\": %.2f, "
223                                  "\"nice\": %.2f, "
224                                  "\"system\": %.2f, "
225                                  "\"iowait\": %.2f, "
226                                  "\"steal\": %.2f, "
227                                  "\"idle\": %.2f}",
228                                  cpuno,
229                                  ll_sp_value(scp->cpu_user, scc->cpu_user, deltot_jiffies),
230                                  ll_sp_value(scp->cpu_nice, scc->cpu_nice, deltot_jiffies),
231                                  ll_sp_value(scp->cpu_sys + scp->cpu_hardirq + scp->cpu_softirq,
232                                              scc->cpu_sys + scc->cpu_hardirq + scc->cpu_softirq,
233                                              deltot_jiffies),
234                                  ll_sp_value(scp->cpu_iowait, scc->cpu_iowait, deltot_jiffies),
235                                  ll_sp_value(scp->cpu_steal, scc->cpu_steal, deltot_jiffies),
236                                  scc->cpu_idle < scp->cpu_idle ?
237                                  0.0 :
238                                  ll_sp_value(scp->cpu_idle, scc->cpu_idle, deltot_jiffies));
239                 }
240                 else if (DISPLAY_CPU_ALL(a->opt_flags)) {
241                         xprintf0(tab, "{\"cpu\": \"%s\", "
242                                  "\"usr\": %.2f, "
243                                  "\"nice\": %.2f, "
244                                  "\"sys\": %.2f, "
245                                  "\"iowait\": %.2f, "
246                                  "\"steal\": %.2f, "
247                                  "\"irq\": %.2f, "
248                                  "\"soft\": %.2f, "
249                                  "\"guest\": %.2f, "
250                                  "\"gnice\": %.2f, "
251                                  "\"idle\": %.2f}",
252                                  cpuno,
253                                  (scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ?
254                                  0.0 :
255                                  ll_sp_value(scp->cpu_user - scp->cpu_guest,
256                                              scc->cpu_user - scc->cpu_guest, deltot_jiffies),
257                                  (scc->cpu_nice - scc->cpu_guest_nice) < (scp->cpu_nice - scp->cpu_guest_nice) ?
258                                  0.0 :
259                                  ll_sp_value(scp->cpu_nice - scp->cpu_guest_nice,
260                                              scc->cpu_nice - scc->cpu_guest_nice, deltot_jiffies),
261                                  ll_sp_value(scp->cpu_sys, scc->cpu_sys, deltot_jiffies),
262                                  ll_sp_value(scp->cpu_iowait, scc->cpu_iowait, deltot_jiffies),
263                                  ll_sp_value(scp->cpu_steal, scc->cpu_steal, deltot_jiffies),
264                                  ll_sp_value(scp->cpu_hardirq, scc->cpu_hardirq, deltot_jiffies),
265                                  ll_sp_value(scp->cpu_softirq, scc->cpu_softirq, deltot_jiffies),
266                                  ll_sp_value(scp->cpu_guest, scc->cpu_guest, deltot_jiffies),
267                                  ll_sp_value(scp->cpu_guest_nice, scc->cpu_guest_nice, deltot_jiffies),
268                                  scc->cpu_idle < scp->cpu_idle ?
269                                  0.0 :
270                                  ll_sp_value(scp->cpu_idle, scc->cpu_idle, deltot_jiffies));
271                 }
272         }
273
274         printf("\n");
275         xprintf0(--tab, "]");
276 }
277
278 /*
279  ***************************************************************************
280  * Display task creation and context switch statistics in JSON.
281  *
282  * IN:
283  * @a           Activity structure with statistics.
284  * @curr        Index in array for current sample statistics.
285  * @tab         Indentation in output.
286  * @itv         Interval of time in 1/100th of a second.
287  ***************************************************************************
288  */
289 __print_funct_t json_print_pcsw_stats(struct activity *a, int curr, int tab,
290                                       unsigned long long itv)
291 {
292         struct stats_pcsw
293                 *spc = (struct stats_pcsw *) a->buf[curr],
294                 *spp = (struct stats_pcsw *) a->buf[!curr];
295
296         /* proc/s and cswch/s */
297         xprintf0(tab, "\"process-and-context-switch\": {"
298                  "\"proc\": %.2f, "
299                  "\"cswch\": %.2f}",
300                  S_VALUE(spp->processes, spc->processes, itv),
301                  S_VALUE(spp->context_switch, spc->context_switch, itv));
302 }
303
304 /*
305  ***************************************************************************
306  * Display interrupts statistics in JSON.
307  *
308  * IN:
309  * @a           Activity structure with statistics.
310  * @curr        Index in array for current sample statistics.
311  * @tab         Indentation in output.
312  * @itv         Interval of time in 1/100th of a second.
313  ***************************************************************************
314  */
315 __print_funct_t json_print_irq_stats(struct activity *a, int curr, int tab,
316                                      unsigned long long itv)
317 {
318         int i;
319         struct stats_irq *sic, *sip;
320         int sep = FALSE;
321         char irqno[8];
322
323         xprintf(tab++, "\"interrupts\": [");
324
325         for (i = 0; (i < a->nr) && (i < a->bitmap->b_size + 1); i++) {
326
327                 sic = (struct stats_irq *) ((char *) a->buf[curr]  + i * a->msize);
328                 sip = (struct stats_irq *) ((char *) a->buf[!curr] + i * a->msize);
329
330                 /* Should current interrupt (including int "sum") be displayed? */
331                 if (a->bitmap->b_array[i >> 3] & (1 << (i & 0x07))) {
332
333                         /* Yes: Display it */
334
335                         if (sep) {
336                                 printf(",\n");
337                         }
338                         sep = TRUE;
339
340                         if (!i) {
341                                 /* This is interrupt "sum" */
342                                 strcpy(irqno, "sum");
343                         }
344                         else {
345                                 sprintf(irqno, "%d", i - 1);
346                         }
347
348                         xprintf0(tab, "{\"intr\": \"%s\", "
349                                  "\"value\": %.2f}",
350                                  irqno,
351                                  S_VALUE(sip->irq_nr, sic->irq_nr, itv));
352                 }
353         }
354
355         printf("\n");
356         xprintf0(--tab, "]");
357 }
358
359 /*
360  ***************************************************************************
361  * Display swapping statistics in JSON.
362  *
363  * IN:
364  * @a           Activity structure with statistics.
365  * @curr        Index in array for current sample statistics.
366  * @tab         Indentation in output.
367  * @itv         Interval of time in 1/100th of a second.
368  ***************************************************************************
369  */
370 __print_funct_t json_print_swap_stats(struct activity *a, int curr, int tab,
371                                       unsigned long long itv)
372 {
373         struct stats_swap
374                 *ssc = (struct stats_swap *) a->buf[curr],
375                 *ssp = (struct stats_swap *) a->buf[!curr];
376
377         xprintf0(tab, "\"swap-pages\": {"
378                  "\"pswpin\": %.2f, "
379                  "\"pswpout\": %.2f}",
380                  S_VALUE(ssp->pswpin,  ssc->pswpin,  itv),
381                  S_VALUE(ssp->pswpout, ssc->pswpout, itv));
382 }
383
384 /*
385  ***************************************************************************
386  * Display paging statistics in JSON.
387  *
388  * IN:
389  * @a           Activity structure with statistics.
390  * @curr        Index in array for current sample statistics.
391  * @tab         Indentation in output.
392  * @itv         Interval of time in 1/100th of a second.
393  ***************************************************************************
394  */
395 __print_funct_t json_print_paging_stats(struct activity *a, int curr, int tab,
396                                         unsigned long long itv)
397 {
398         struct stats_paging
399                 *spc = (struct stats_paging *) a->buf[curr],
400                 *spp = (struct stats_paging *) a->buf[!curr];
401
402         xprintf0(tab, "\"paging\": {"
403                  "\"pgpgin\": %.2f, "
404                  "\"pgpgout\": %.2f, "
405                  "\"fault\": %.2f, "
406                  "\"majflt\": %.2f, "
407                  "\"pgfree\": %.2f, "
408                  "\"pgscank\": %.2f, "
409                  "\"pgscand\": %.2f, "
410                  "\"pgsteal\": %.2f, "
411                  "\"vmeff-percent\": %.2f}",
412                  S_VALUE(spp->pgpgin,        spc->pgpgin,        itv),
413                  S_VALUE(spp->pgpgout,       spc->pgpgout,       itv),
414                  S_VALUE(spp->pgfault,       spc->pgfault,       itv),
415                  S_VALUE(spp->pgmajfault,    spc->pgmajfault,    itv),
416                  S_VALUE(spp->pgfree,        spc->pgfree,        itv),
417                  S_VALUE(spp->pgscan_kswapd, spc->pgscan_kswapd, itv),
418                  S_VALUE(spp->pgscan_direct, spc->pgscan_direct, itv),
419                  S_VALUE(spp->pgsteal,       spc->pgsteal,       itv),
420                  (spc->pgscan_kswapd + spc->pgscan_direct -
421                   spp->pgscan_kswapd - spp->pgscan_direct) ?
422                  SP_VALUE(spp->pgsteal, spc->pgsteal,
423                           spc->pgscan_kswapd + spc->pgscan_direct -
424                           spp->pgscan_kswapd - spp->pgscan_direct) : 0.0);
425 }
426
427 /*
428  ***************************************************************************
429  * Display I/O and transfer rate statistics in JSON.
430  *
431  * IN:
432  * @a           Activity structure with statistics.
433  * @curr        Index in array for current sample statistics.
434  * @tab         Indentation in output.
435  * @itv         Interval of time in 1/100th of a second.
436  ***************************************************************************
437  */
438 __print_funct_t json_print_io_stats(struct activity *a, int curr, int tab,
439                                     unsigned long long itv)
440 {
441         struct stats_io
442                 *sic = (struct stats_io *) a->buf[curr],
443                 *sip = (struct stats_io *) a->buf[!curr];
444
445         xprintf0(tab, "\"io\": {"
446                  "\"tps\": %.2f, "
447                  "\"io-reads\": {"
448                  "\"rtps\": %.2f, "
449                  "\"bread\": %.2f}, "
450                  "\"io-writes\": {"
451                  "\"wtps\": %.2f, "
452                  "\"bwrtn\": %.2f}}",
453                  /*
454                   * If we get negative values, this is probably because
455                   * one or more devices/filesystems have been unmounted.
456                   * We display 0.0 in this case though we should rather tell
457                   * the user that the value cannot be calculated here.
458                   */
459                  sic->dk_drive < sip->dk_drive ? 0.0 :
460                  S_VALUE(sip->dk_drive, sic->dk_drive, itv),
461                  sic->dk_drive_rio < sip->dk_drive_rio ? 0.0 :
462                  S_VALUE(sip->dk_drive_rio, sic->dk_drive_rio, itv),
463                  sic->dk_drive_rblk < sip->dk_drive_rblk ? 0.0 :
464                  S_VALUE(sip->dk_drive_rblk, sic->dk_drive_rblk, itv),
465                  sic->dk_drive_wio < sip->dk_drive_wio ? 0.0 :
466                  S_VALUE(sip->dk_drive_wio, sic->dk_drive_wio, itv),
467                  sic->dk_drive_wblk < sip->dk_drive_wblk ? 0.0 :
468                  S_VALUE(sip->dk_drive_wblk, sic->dk_drive_wblk, itv));
469 }
470
471 /*
472  ***************************************************************************
473  * Display memory statistics in JSON.
474  *
475  * IN:
476  * @a           Activity structure with statistics.
477  * @curr        Index in array for current sample statistics.
478  * @tab         Indentation in output.
479  * @itv         Interval of time in 1/100th of a second.
480  ***************************************************************************
481  */
482 __print_funct_t json_print_memory_stats(struct activity *a, int curr, int tab,
483                                         unsigned long long itv)
484 {
485         struct stats_memory
486                 *smc = (struct stats_memory *) a->buf[curr];
487         int sep = FALSE;
488
489         xprintf0(tab, "\"memory\": {");
490
491         if (DISPLAY_MEMORY(a->opt_flags)) {
492
493                 sep = TRUE;
494
495                 printf("\"memfree\": %lu, "
496                        "\"avail\": %lu, "
497                        "\"memused\": %lu, "
498                        "\"memused-percent\": %.2f, "
499                        "\"buffers\": %lu, "
500                        "\"cached\": %lu, "
501                        "\"commit\": %lu, "
502                        "\"commit-percent\": %.2f, "
503                        "\"active\": %lu, "
504                        "\"inactive\": %lu, "
505                        "\"dirty\": %lu",
506                        smc->frmkb,
507                        smc->availablekb,
508                        smc->tlmkb - smc->frmkb,
509                        smc->tlmkb ?
510                        SP_VALUE(smc->frmkb, smc->tlmkb, smc->tlmkb) :
511                        0.0,
512                        smc->bufkb,
513                        smc->camkb,
514                        smc->comkb,
515                        (smc->tlmkb + smc->tlskb) ?
516                        SP_VALUE(0, smc->comkb, smc->tlmkb + smc->tlskb) :
517                        0.0,
518                        smc->activekb,
519                        smc->inactkb,
520                        smc->dirtykb);
521
522                 if (DISPLAY_MEM_ALL(a->opt_flags)) {
523                         /* Display extended memory stats */
524                         printf(", \"anonpg\": %lu, "
525                                "\"slab\": %lu, "
526                                "\"kstack\": %lu, "
527                                "\"pgtbl\": %lu, "
528                                "\"vmused\": %lu",
529                                smc->anonpgkb,
530                                smc->slabkb,
531                                smc->kstackkb,
532                                smc->pgtblkb,
533                                smc->vmusedkb);
534                 }
535         }
536
537         if (DISPLAY_SWAP(a->opt_flags)) {
538
539                 if (sep) {
540                         printf(", ");
541                 }
542                 sep = TRUE;
543
544                 printf("\"swpfree\": %lu, "
545                        "\"swpused\": %lu, "
546                        "\"swpused-percent\": %.2f, "
547                        "\"swpcad\": %lu, "
548                        "\"swpcad-percent\": %.2f",
549                        smc->frskb,
550                        smc->tlskb - smc->frskb,
551                        smc->tlskb ?
552                        SP_VALUE(smc->frskb, smc->tlskb, smc->tlskb) :
553                        0.0,
554                        smc->caskb,
555                        (smc->tlskb - smc->frskb) ?
556                        SP_VALUE(0, smc->caskb, smc->tlskb - smc->frskb) :
557                        0.0);
558         }
559
560         printf("}");
561 }
562
563 /*
564  ***************************************************************************
565  * Display kernel tables statistics in JSON.
566  *
567  * IN:
568  * @a           Activity structure with statistics.
569  * @curr        Index in array for current sample statistics.
570  * @tab         Indentation in output.
571  * @itv         Interval of time in 1/100th of a second.
572  ***************************************************************************
573  */
574 __print_funct_t json_print_ktables_stats(struct activity *a, int curr, int tab,
575                                          unsigned long long itv)
576 {
577         struct stats_ktables
578                 *skc = (struct stats_ktables *) a->buf[curr];
579
580         xprintf0(tab, "\"kernel\": {"
581                  "\"dentunusd\": %u, "
582                  "\"file-nr\": %u, "
583                  "\"inode-nr\": %u, "
584                  "\"pty-nr\": %u}",
585                  skc->dentry_stat,
586                  skc->file_used,
587                  skc->inode_used,
588                  skc->pty_nr);
589 }
590
591 /*
592  ***************************************************************************
593  * Display queue and load statistics in JSON.
594  *
595  * IN:
596  * @a           Activity structure with statistics.
597  * @curr        Index in array for current sample statistics.
598  * @tab         Indentation in output.
599  * @itv         Interval of time in 1/100th of a second.
600  ***************************************************************************
601  */
602 __print_funct_t json_print_queue_stats(struct activity *a, int curr, int tab,
603                                        unsigned long long itv)
604 {
605         struct stats_queue
606                 *sqc = (struct stats_queue *) a->buf[curr];
607
608         xprintf0(tab, "\"queue\": {"
609                  "\"runq-sz\": %lu, "
610                  "\"plist-sz\": %u, "
611                  "\"ldavg-1\": %.2f, "
612                  "\"ldavg-5\": %.2f, "
613                  "\"ldavg-15\": %.2f, "
614                  "\"blocked\": %lu}",
615                  sqc->nr_running,
616                  sqc->nr_threads,
617                  (double) sqc->load_avg_1 / 100,
618                  (double) sqc->load_avg_5 / 100,
619                  (double) sqc->load_avg_15 / 100,
620                  sqc->procs_blocked);
621 }
622
623 /*
624  ***************************************************************************
625  * Display serial lines statistics in JSON.
626  *
627  * IN:
628  * @a           Activity structure with statistics.
629  * @curr        Index in array for current sample statistics.
630  * @tab         Indentation in output.
631  * @itv         Interval of time in 1/100th of a second.
632  ***************************************************************************
633  */
634 __print_funct_t json_print_serial_stats(struct activity *a, int curr, int tab,
635                                         unsigned long long itv)
636 {
637         int i;
638         struct stats_serial *ssc, *ssp;
639         int sep = FALSE;
640
641         xprintf(tab++, "\"serial\": [");
642
643         for (i = 0; i < a->nr; i++) {
644
645                 ssc = (struct stats_serial *) ((char *) a->buf[curr]  + i * a->msize);
646                 ssp = (struct stats_serial *) ((char *) a->buf[!curr] + i * a->msize);
647
648                 if (ssc->line == 0)
649                         continue;
650
651                 if (ssc->line == ssp->line) {
652
653                         if (sep) {
654                                 printf(",\n");
655                         }
656                         sep = TRUE;
657
658                         xprintf0(tab, "{\"line\": %d, "
659                                  "\"rcvin\": %.2f, "
660                                  "\"xmtin\": %.2f, "
661                                  "\"framerr\": %.2f, "
662                                  "\"prtyerr\": %.2f, "
663                                  "\"brk\": %.2f, "
664                                  "\"ovrun\": %.2f}",
665                                  ssc->line - 1,
666                                  S_VALUE(ssp->rx,      ssc->rx,      itv),
667                                  S_VALUE(ssp->tx,      ssc->tx,      itv),
668                                  S_VALUE(ssp->frame,   ssc->frame,   itv),
669                                  S_VALUE(ssp->parity,  ssc->parity,  itv),
670                                  S_VALUE(ssp->brk,     ssc->brk,     itv),
671                                  S_VALUE(ssp->overrun, ssc->overrun, itv));
672                 }
673         }
674
675         printf("\n");
676         xprintf0(--tab, "]");
677 }
678
679 /*
680  ***************************************************************************
681  * Display disks statistics in JSON.
682  *
683  * IN:
684  * @a           Activity structure with statistics.
685  * @curr        Index in array for current sample statistics.
686  * @tab         Indentation in output.
687  * @itv         Interval of time in 1/100th of a second.
688  ***************************************************************************
689  */
690 __print_funct_t json_print_disk_stats(struct activity *a, int curr, int tab,
691                                       unsigned long long itv)
692 {
693         int i, j;
694         struct stats_disk *sdc, *sdp, sdpzero;
695         struct ext_disk_stats xds;
696         int sep = FALSE;
697         char *dev_name, *persist_dev_name;
698
699         memset(&sdpzero, 0, STATS_DISK_SIZE);
700
701         xprintf(tab++, "\"disk\": [");
702
703         for (i = 0; i < a->nr; i++) {
704
705                 sdc = (struct stats_disk *) ((char *) a->buf[curr] + i * a->msize);
706
707                 if (!(sdc->major + sdc->minor))
708                         continue;
709
710                 j = check_disk_reg(a, curr, !curr, i);
711                 if (j < 0) {
712                         /* This is a newly registered interface. Previous stats are zero */
713                         sdp = &sdpzero;
714                 }
715                 else {
716                         sdp = (struct stats_disk *) ((char *) a->buf[!curr] + j * a->msize);
717                 }
718
719                 /* Compute extended statistics values */
720                 compute_ext_disk_stats(sdc, sdp, itv, &xds);
721
722                 dev_name = NULL;
723                 persist_dev_name = NULL;
724
725                 if (DISPLAY_PERSIST_NAME_S(flags)) {
726                         persist_dev_name = get_persistent_name_from_pretty(get_devname(sdc->major, sdc->minor, TRUE));
727                 }
728
729                 if (persist_dev_name) {
730                         dev_name = persist_dev_name;
731                 }
732                 else {
733                         if ((USE_PRETTY_OPTION(flags)) && (sdc->major == dm_major)) {
734                                 dev_name = transform_devmapname(sdc->major, sdc->minor);
735                         }
736
737                         if (!dev_name) {
738                                 dev_name = get_devname(sdc->major, sdc->minor,
739                                                        USE_PRETTY_OPTION(flags));
740                         }
741                 }
742
743                 if (sep) {
744                         printf(",\n");
745                 }
746                 sep = TRUE;
747
748                 xprintf0(tab, "{\"disk-device\": \"%s\", "
749                          "\"tps\": %.2f, "
750                          "\"rd_sec\": %.2f, "
751                          "\"wr_sec\": %.2f, "
752                          "\"rkB\": %.2f, "
753                          "\"wkB\": %.2f, "
754                          "\"avgrq-sz\": %.2f, "
755                          "\"areq-sz\": %.2f, "
756                          "\"avgqu-sz\": %.2f, "
757                          "\"aqu-sz\": %.2f, "
758                          "\"await\": %.2f, "
759                          "\"svctm\": %.2f, "
760                          "\"util-percent\": %.2f}",
761                          /* Confusion possible here between index and minor numbers */
762                          dev_name,
763                          S_VALUE(sdp->nr_ios, sdc->nr_ios, itv),
764                          S_VALUE(sdp->rd_sect, sdc->rd_sect, itv), /* Unit = sectors (for backward compatibility) */
765                          S_VALUE(sdp->wr_sect, sdc->wr_sect, itv),
766                          S_VALUE(sdp->rd_sect, sdc->rd_sect, itv) / 2,
767                          S_VALUE(sdp->wr_sect, sdc->wr_sect, itv) / 2,
768                          /* See iostat for explanations */
769                          xds.arqsz,     /* Unit = sectors (for backward compatibility) */
770                          xds.arqsz / 2,
771                          S_VALUE(sdp->rq_ticks, sdc->rq_ticks, itv) / 1000.0,   /* For backward compatibility */
772                          S_VALUE(sdp->rq_ticks, sdc->rq_ticks, itv) / 1000.0,
773                          xds.await,
774                          xds.svctm,
775                          xds.util / 10.0);
776         }
777
778         printf("\n");
779         xprintf0(--tab, "]");
780 }
781
782 /*
783  ***************************************************************************
784  * Display network interfaces statistics in JSON.
785  *
786  * IN:
787  * @a           Activity structure with statistics.
788  * @curr        Index in array for current sample statistics.
789  * @tab         Indentation in output.
790  * @itv         Interval of time in 1/100th of a second.
791  ***************************************************************************
792  */
793 __print_funct_t json_print_net_dev_stats(struct activity *a, int curr, int tab,
794                                          unsigned long long itv)
795 {
796         int i, j;
797         struct stats_net_dev *sndc, *sndp, sndzero;
798         int sep = FALSE;
799         double rxkb, txkb, ifutil;
800
801         memset(&sndzero, 0, STATS_NET_DEV_SIZE);
802
803         if (!IS_SELECTED(a->options) || (a->nr <= 0))
804                 goto close_json_markup;
805
806         json_markup_network(tab, OPEN_JSON_MARKUP);
807         tab++;
808
809         xprintf(tab++, "\"net-dev\": [");
810
811         for (i = 0; i < a->nr; i++) {
812
813                 sndc = (struct stats_net_dev *) ((char *) a->buf[curr] + i * a->msize);
814
815                 if (!strcmp(sndc->interface, ""))
816                         break;
817
818                 j = check_net_dev_reg(a, curr, !curr, i);
819                 if (j < 0) {
820                         /* This is a newly registered interface. Previous stats are zero */
821                         sndp = &sndzero;
822                 }
823                 else {
824                         sndp = (struct stats_net_dev *) ((char *) a->buf[!curr] + j * a->msize);
825                 }
826
827                 if (sep) {
828                         printf(",\n");
829                 }
830                 sep = TRUE;
831
832                 rxkb = S_VALUE(sndp->rx_bytes, sndc->rx_bytes, itv);
833                 txkb = S_VALUE(sndp->tx_bytes, sndc->tx_bytes, itv);
834                 ifutil = compute_ifutil(sndc, rxkb, txkb);
835
836                 xprintf0(tab, "{\"iface\": \"%s\", "
837                          "\"rxpck\": %.2f, "
838                          "\"txpck\": %.2f, "
839                          "\"rxkB\": %.2f, "
840                          "\"txkB\": %.2f, "
841                          "\"rxcmp\": %.2f, "
842                          "\"txcmp\": %.2f, "
843                          "\"rxmcst\": %.2f, "
844                          "\"ifutil-percent\": %.2f}",
845                          sndc->interface,
846                          S_VALUE(sndp->rx_packets,    sndc->rx_packets,    itv),
847                          S_VALUE(sndp->tx_packets,    sndc->tx_packets,    itv),
848                          rxkb / 1024,
849                          txkb / 1024,
850                          S_VALUE(sndp->rx_compressed, sndc->rx_compressed, itv),
851                          S_VALUE(sndp->tx_compressed, sndc->tx_compressed, itv),
852                          S_VALUE(sndp->multicast,     sndc->multicast,     itv),
853                          ifutil);
854         }
855
856         printf("\n");
857         xprintf0(--tab, "]");
858
859         tab--;
860
861 close_json_markup:
862         if (CLOSE_MARKUP(a->options)) {
863                 json_markup_network(tab, CLOSE_JSON_MARKUP);
864         }
865 }
866
867 /*
868  ***************************************************************************
869  * Display network interfaces errors statistics in JSON.
870  *
871  * IN:
872  * @a           Activity structure with statistics.
873  * @curr        Index in array for current sample statistics.
874  * @tab         Indentation in output.
875  * @itv         Interval of time in 1/100th of a second.
876  ***************************************************************************
877  */
878 __print_funct_t json_print_net_edev_stats(struct activity *a, int curr, int tab,
879                                           unsigned long long itv)
880 {
881         int i, j;
882         struct stats_net_edev *snedc, *snedp, snedzero;
883         int sep = FALSE;
884
885         if (!IS_SELECTED(a->options) || (a->nr <= 0))
886                 goto close_json_markup;
887
888         memset(&snedzero, 0, STATS_NET_EDEV_SIZE);
889
890         json_markup_network(tab, OPEN_JSON_MARKUP);
891         tab++;
892
893         xprintf(tab++, "\"net-edev\": [");
894
895         for (i = 0; i < a->nr; i++) {
896
897                 snedc = (struct stats_net_edev *) ((char *) a->buf[curr] + i * a->msize);
898
899                 if (!strcmp(snedc->interface, ""))
900                         break;
901
902                 j = check_net_edev_reg(a, curr, !curr, i);
903                 if (j < 0) {
904                         /* This is a newly registered interface. Previous stats are zero */
905                         snedp = &snedzero;
906                 }
907                 else {
908                         snedp = (struct stats_net_edev *) ((char *) a->buf[!curr] + j * a->msize);
909                 }
910
911                 if (sep) {
912                         printf(",\n");
913                 }
914                 sep = TRUE;
915
916                 xprintf0(tab, "{\"iface\": \"%s\", "
917                          "\"rxerr\": %.2f, "
918                          "\"txerr\": %.2f, "
919                          "\"coll\": %.2f, "
920                          "\"rxdrop\": %.2f, "
921                          "\"txdrop\": %.2f, "
922                          "\"txcarr\": %.2f, "
923                          "\"rxfram\": %.2f, "
924                          "\"rxfifo\": %.2f, "
925                          "\"txfifo\": %.2f}",
926                          snedc->interface,
927                          S_VALUE(snedp->rx_errors,         snedc->rx_errors,         itv),
928                          S_VALUE(snedp->tx_errors,         snedc->tx_errors,         itv),
929                          S_VALUE(snedp->collisions,        snedc->collisions,        itv),
930                          S_VALUE(snedp->rx_dropped,        snedc->rx_dropped,        itv),
931                          S_VALUE(snedp->tx_dropped,        snedc->tx_dropped,        itv),
932                          S_VALUE(snedp->tx_carrier_errors, snedc->tx_carrier_errors, itv),
933                          S_VALUE(snedp->rx_frame_errors,   snedc->rx_frame_errors,   itv),
934                          S_VALUE(snedp->rx_fifo_errors,    snedc->rx_fifo_errors,    itv),
935                          S_VALUE(snedp->tx_fifo_errors,    snedc->tx_fifo_errors,    itv));
936         }
937
938         printf("\n");
939         xprintf0(--tab, "]");
940
941         tab--;
942
943 close_json_markup:
944         if (CLOSE_MARKUP(a->options)) {
945                 json_markup_network(tab, CLOSE_JSON_MARKUP);
946         }
947 }
948
949 /*
950  ***************************************************************************
951  * Display NFS client statistics in JSON.
952  *
953  * IN:
954  * @a           Activity structure with statistics.
955  * @curr        Index in array for current sample statistics.
956  * @tab         Indentation in output.
957  * @itv         Interval of time in 1/100th of a second.
958  ***************************************************************************
959  */
960 __print_funct_t json_print_net_nfs_stats(struct activity *a, int curr, int tab,
961                                          unsigned long long itv)
962 {
963         struct stats_net_nfs
964                 *snnc = (struct stats_net_nfs *) a->buf[curr],
965                 *snnp = (struct stats_net_nfs *) a->buf[!curr];
966
967         if (!IS_SELECTED(a->options) || (a->nr <= 0))
968                 goto close_json_markup;
969
970         json_markup_network(tab, OPEN_JSON_MARKUP);
971         tab++;
972
973         xprintf0(tab, "\"net-nfs\": {"
974                  "\"call\": %.2f, "
975                  "\"retrans\": %.2f, "
976                  "\"read\": %.2f, "
977                  "\"write\": %.2f, "
978                  "\"access\": %.2f, "
979                  "\"getatt\": %.2f}",
980                  S_VALUE(snnp->nfs_rpccnt,     snnc->nfs_rpccnt,     itv),
981                  S_VALUE(snnp->nfs_rpcretrans, snnc->nfs_rpcretrans, itv),
982                  S_VALUE(snnp->nfs_readcnt,    snnc->nfs_readcnt,    itv),
983                  S_VALUE(snnp->nfs_writecnt,   snnc->nfs_writecnt,   itv),
984                  S_VALUE(snnp->nfs_accesscnt,  snnc->nfs_accesscnt,  itv),
985                  S_VALUE(snnp->nfs_getattcnt,  snnc->nfs_getattcnt,  itv));
986         tab--;
987
988 close_json_markup:
989         if (CLOSE_MARKUP(a->options)) {
990                 json_markup_network(tab, CLOSE_JSON_MARKUP);
991         }
992 }
993
994 /*
995  ***************************************************************************
996  * Display NFS server statistics in JSON.
997  *
998  * IN:
999  * @a           Activity structure with statistics.
1000  * @curr        Index in array for current sample statistics.
1001  * @tab         Indentation in output.
1002  * @itv         Interval of time in 1/100th of a second.
1003  ***************************************************************************
1004  */
1005 __print_funct_t json_print_net_nfsd_stats(struct activity *a, int curr, int tab,
1006                                           unsigned long long itv)
1007 {
1008         struct stats_net_nfsd
1009                 *snndc = (struct stats_net_nfsd *) a->buf[curr],
1010                 *snndp = (struct stats_net_nfsd *) a->buf[!curr];
1011
1012         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1013                 goto close_json_markup;
1014
1015         json_markup_network(tab, OPEN_JSON_MARKUP);
1016         tab++;
1017
1018         xprintf0(tab, "\"net-nfsd\": {"
1019                  "\"scall\": %.2f, "
1020                  "\"badcall\": %.2f, "
1021                  "\"packet\": %.2f, "
1022                  "\"udp\": %.2f, "
1023                  "\"tcp\": %.2f, "
1024                  "\"hit\": %.2f, "
1025                  "\"miss\": %.2f, "
1026                  "\"sread\": %.2f, "
1027                  "\"swrite\": %.2f, "
1028                  "\"saccess\": %.2f, "
1029                  "\"sgetatt\": %.2f}",
1030                  S_VALUE(snndp->nfsd_rpccnt,    snndc->nfsd_rpccnt,    itv),
1031                  S_VALUE(snndp->nfsd_rpcbad,    snndc->nfsd_rpcbad,    itv),
1032                  S_VALUE(snndp->nfsd_netcnt,    snndc->nfsd_netcnt,    itv),
1033                  S_VALUE(snndp->nfsd_netudpcnt, snndc->nfsd_netudpcnt, itv),
1034                  S_VALUE(snndp->nfsd_nettcpcnt, snndc->nfsd_nettcpcnt, itv),
1035                  S_VALUE(snndp->nfsd_rchits,    snndc->nfsd_rchits,    itv),
1036                  S_VALUE(snndp->nfsd_rcmisses,  snndc->nfsd_rcmisses,  itv),
1037                  S_VALUE(snndp->nfsd_readcnt,   snndc->nfsd_readcnt,   itv),
1038                  S_VALUE(snndp->nfsd_writecnt,  snndc->nfsd_writecnt,  itv),
1039                  S_VALUE(snndp->nfsd_accesscnt, snndc->nfsd_accesscnt, itv),
1040                  S_VALUE(snndp->nfsd_getattcnt, snndc->nfsd_getattcnt, itv));
1041         tab--;
1042
1043 close_json_markup:
1044         if (CLOSE_MARKUP(a->options)) {
1045                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1046         }
1047 }
1048
1049 /*
1050  ***************************************************************************
1051  * Display network socket statistics in JSON.
1052  *
1053  * IN:
1054  * @a           Activity structure with statistics.
1055  * @curr        Index in array for current sample statistics.
1056  * @tab         Indentation in output.
1057  * @itv         Interval of time in 1/100th of a second.
1058  ***************************************************************************
1059  */
1060 __print_funct_t json_print_net_sock_stats(struct activity *a, int curr, int tab,
1061                                           unsigned long long itv)
1062 {
1063         struct stats_net_sock
1064                 *snsc = (struct stats_net_sock *) a->buf[curr];
1065
1066         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1067                 goto close_json_markup;
1068
1069         json_markup_network(tab, OPEN_JSON_MARKUP);
1070         tab++;
1071
1072         xprintf0(tab, "\"net-sock\": {"
1073                  "\"totsck\": %u, "
1074                  "\"tcpsck\": %u, "
1075                  "\"udpsck\": %u, "
1076                  "\"rawsck\": %u, "
1077                  "\"ip-frag\": %u, "
1078                  "\"tcp-tw\": %u}",
1079                  snsc->sock_inuse,
1080                  snsc->tcp_inuse,
1081                  snsc->udp_inuse,
1082                  snsc->raw_inuse,
1083                  snsc->frag_inuse,
1084                  snsc->tcp_tw);
1085         tab--;
1086
1087 close_json_markup:
1088         if (CLOSE_MARKUP(a->options)) {
1089                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1090         }
1091 }
1092
1093 /*
1094  ***************************************************************************
1095  * Display IP network statistics in JSON.
1096  *
1097  * IN:
1098  * @a           Activity structure with statistics.
1099  * @curr        Index in array for current sample statistics.
1100  * @tab         Indentation in output.
1101  * @itv         Interval of time in 1/100th of a second.
1102  ***************************************************************************
1103  */
1104 __print_funct_t json_print_net_ip_stats(struct activity *a, int curr, int tab,
1105                                         unsigned long long itv)
1106 {
1107         struct stats_net_ip
1108                 *snic = (struct stats_net_ip *) a->buf[curr],
1109                 *snip = (struct stats_net_ip *) a->buf[!curr];
1110
1111         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1112                 goto close_json_markup;
1113
1114         json_markup_network(tab, OPEN_JSON_MARKUP);
1115         tab++;
1116
1117         xprintf0(tab, "\"net-ip\": {"
1118                  "\"irec\": %.2f, "
1119                  "\"fwddgm\": %.2f, "
1120                  "\"idel\": %.2f, "
1121                  "\"orq\": %.2f, "
1122                  "\"asmrq\": %.2f, "
1123                  "\"asmok\": %.2f, "
1124                  "\"fragok\": %.2f, "
1125                  "\"fragcrt\": %.2f}",
1126                  S_VALUE(snip->InReceives,    snic->InReceives,    itv),
1127                  S_VALUE(snip->ForwDatagrams, snic->ForwDatagrams, itv),
1128                  S_VALUE(snip->InDelivers,    snic->InDelivers,    itv),
1129                  S_VALUE(snip->OutRequests,   snic->OutRequests,   itv),
1130                  S_VALUE(snip->ReasmReqds,    snic->ReasmReqds,    itv),
1131                  S_VALUE(snip->ReasmOKs,      snic->ReasmOKs,      itv),
1132                  S_VALUE(snip->FragOKs,       snic->FragOKs,       itv),
1133                  S_VALUE(snip->FragCreates,   snic->FragCreates,   itv));
1134         tab--;
1135
1136 close_json_markup:
1137         if (CLOSE_MARKUP(a->options)) {
1138                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1139         }
1140 }
1141
1142 /*
1143  ***************************************************************************
1144  * Display IP network errors statistics in JSON.
1145  *
1146  * IN:
1147  * @a           Activity structure with statistics.
1148  * @curr        Index in array for current sample statistics.
1149  * @tab         Indentation in output.
1150  * @itv         Interval of time in 1/100th of a second.
1151  ***************************************************************************
1152  */
1153 __print_funct_t json_print_net_eip_stats(struct activity *a, int curr, int tab,
1154                                          unsigned long long itv)
1155 {
1156         struct stats_net_eip
1157                 *sneic = (struct stats_net_eip *) a->buf[curr],
1158                 *sneip = (struct stats_net_eip *) a->buf[!curr];
1159
1160         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1161                 goto close_json_markup;
1162
1163         json_markup_network(tab, OPEN_JSON_MARKUP);
1164         tab++;
1165
1166         xprintf0(tab, "\"net-eip\": {"
1167                  "\"ihdrerr\": %.2f, "
1168                  "\"iadrerr\": %.2f, "
1169                  "\"iukwnpr\": %.2f, "
1170                  "\"idisc\": %.2f, "
1171                  "\"odisc\": %.2f, "
1172                  "\"onort\": %.2f, "
1173                  "\"asmf\": %.2f, "
1174                  "\"fragf\": %.2f}",
1175                  S_VALUE(sneip->InHdrErrors,     sneic->InHdrErrors,     itv),
1176                  S_VALUE(sneip->InAddrErrors,    sneic->InAddrErrors,    itv),
1177                  S_VALUE(sneip->InUnknownProtos, sneic->InUnknownProtos, itv),
1178                  S_VALUE(sneip->InDiscards,      sneic->InDiscards,      itv),
1179                  S_VALUE(sneip->OutDiscards,     sneic->OutDiscards,     itv),
1180                  S_VALUE(sneip->OutNoRoutes,     sneic->OutNoRoutes,     itv),
1181                  S_VALUE(sneip->ReasmFails,      sneic->ReasmFails,      itv),
1182                  S_VALUE(sneip->FragFails,       sneic->FragFails,       itv));
1183         tab--;
1184
1185 close_json_markup:
1186         if (CLOSE_MARKUP(a->options)) {
1187                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1188         }
1189 }
1190
1191 /*
1192  ***************************************************************************
1193  * Display ICMP network statistics in JSON.
1194  *
1195  * IN:
1196  * @a           Activity structure with statistics.
1197  * @curr        Index in array for current sample statistics.
1198  * @tab         Indentation in output.
1199  * @itv         Interval of time in 1/100th of a second.
1200  ***************************************************************************
1201  */
1202 __print_funct_t json_print_net_icmp_stats(struct activity *a, int curr, int tab,
1203                                           unsigned long long itv)
1204 {
1205         struct stats_net_icmp
1206                 *snic = (struct stats_net_icmp *) a->buf[curr],
1207                 *snip = (struct stats_net_icmp *) a->buf[!curr];
1208
1209         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1210                 goto close_json_markup;
1211
1212         json_markup_network(tab, OPEN_JSON_MARKUP);
1213         tab++;
1214
1215         xprintf0(tab, "\"net-icmp\": {"
1216                  "\"imsg\": %.2f, "
1217                  "\"omsg\": %.2f, "
1218                  "\"iech\": %.2f, "
1219                  "\"iechr\": %.2f, "
1220                  "\"oech\": %.2f, "
1221                  "\"oechr\": %.2f, "
1222                  "\"itm\": %.2f, "
1223                  "\"itmr\": %.2f, "
1224                  "\"otm\": %.2f, "
1225                  "\"otmr\": %.2f, "
1226                  "\"iadrmk\": %.2f, "
1227                  "\"iadrmkr\": %.2f, "
1228                  "\"oadrmk\": %.2f, "
1229                  "\"oadrmkr\": %.2f}",
1230                  S_VALUE(snip->InMsgs,           snic->InMsgs,           itv),
1231                  S_VALUE(snip->OutMsgs,          snic->OutMsgs,          itv),
1232                  S_VALUE(snip->InEchos,          snic->InEchos,          itv),
1233                  S_VALUE(snip->InEchoReps,       snic->InEchoReps,       itv),
1234                  S_VALUE(snip->OutEchos,         snic->OutEchos,         itv),
1235                  S_VALUE(snip->OutEchoReps,      snic->OutEchoReps,      itv),
1236                  S_VALUE(snip->InTimestamps,     snic->InTimestamps,     itv),
1237                  S_VALUE(snip->InTimestampReps,  snic->InTimestampReps,  itv),
1238                  S_VALUE(snip->OutTimestamps,    snic->OutTimestamps,    itv),
1239                  S_VALUE(snip->OutTimestampReps, snic->OutTimestampReps, itv),
1240                  S_VALUE(snip->InAddrMasks,      snic->InAddrMasks,      itv),
1241                  S_VALUE(snip->InAddrMaskReps,   snic->InAddrMaskReps,   itv),
1242                  S_VALUE(snip->OutAddrMasks,     snic->OutAddrMasks,     itv),
1243                  S_VALUE(snip->OutAddrMaskReps,  snic->OutAddrMaskReps,  itv));
1244         tab--;
1245
1246 close_json_markup:
1247         if (CLOSE_MARKUP(a->options)) {
1248                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1249         }
1250 }
1251
1252 /*
1253  ***************************************************************************
1254  * Display ICMP errors message statistics in JSON.
1255  *
1256  * IN:
1257  * @a           Activity structure with statistics.
1258  * @curr        Index in array for current sample statistics.
1259  * @tab         Indentation in output.
1260  * @itv         Interval of time in 1/100th of a second.
1261  ***************************************************************************
1262  */
1263 __print_funct_t json_print_net_eicmp_stats(struct activity *a, int curr, int tab,
1264                                            unsigned long long itv)
1265 {
1266         struct stats_net_eicmp
1267                 *sneic = (struct stats_net_eicmp *) a->buf[curr],
1268                 *sneip = (struct stats_net_eicmp *) a->buf[!curr];
1269
1270         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1271                 goto close_json_markup;
1272
1273         json_markup_network(tab, OPEN_JSON_MARKUP);
1274         tab++;
1275
1276         xprintf0(tab, "\"net-eicmp\": {"
1277                  "\"ierr\": %.2f, "
1278                  "\"oerr\": %.2f, "
1279                  "\"idstunr\": %.2f, "
1280                  "\"odstunr\": %.2f, "
1281                  "\"itmex\": %.2f, "
1282                  "\"otmex\": %.2f, "
1283                  "\"iparmpb\": %.2f, "
1284                  "\"oparmpb\": %.2f, "
1285                  "\"isrcq\": %.2f, "
1286                  "\"osrcq\": %.2f, "
1287                  "\"iredir\": %.2f, "
1288                  "\"oredir\": %.2f}",
1289                  S_VALUE(sneip->InErrors,        sneic->InErrors,        itv),
1290                  S_VALUE(sneip->OutErrors,       sneic->OutErrors,       itv),
1291                  S_VALUE(sneip->InDestUnreachs,  sneic->InDestUnreachs,  itv),
1292                  S_VALUE(sneip->OutDestUnreachs, sneic->OutDestUnreachs, itv),
1293                  S_VALUE(sneip->InTimeExcds,     sneic->InTimeExcds,     itv),
1294                  S_VALUE(sneip->OutTimeExcds,    sneic->OutTimeExcds,    itv),
1295                  S_VALUE(sneip->InParmProbs,     sneic->InParmProbs,     itv),
1296                  S_VALUE(sneip->OutParmProbs,    sneic->OutParmProbs,    itv),
1297                  S_VALUE(sneip->InSrcQuenchs,    sneic->InSrcQuenchs,    itv),
1298                  S_VALUE(sneip->OutSrcQuenchs,   sneic->OutSrcQuenchs,   itv),
1299                  S_VALUE(sneip->InRedirects,     sneic->InRedirects,     itv),
1300                  S_VALUE(sneip->OutRedirects,    sneic->OutRedirects,    itv));
1301         tab--;
1302
1303 close_json_markup:
1304         if (CLOSE_MARKUP(a->options)) {
1305                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1306         }
1307 }
1308
1309 /*
1310  ***************************************************************************
1311  * Display TCP network statistics in JSON.
1312  *
1313  * IN:
1314  * @a           Activity structure with statistics.
1315  * @curr        Index in array for current sample statistics.
1316  * @tab         Indentation in output.
1317  * @itv         Interval of time in 1/100th of a second.
1318  ***************************************************************************
1319  */
1320 __print_funct_t json_print_net_tcp_stats(struct activity *a, int curr, int tab,
1321                                          unsigned long long itv)
1322 {
1323         struct stats_net_tcp
1324                 *sntc = (struct stats_net_tcp *) a->buf[curr],
1325                 *sntp = (struct stats_net_tcp *) a->buf[!curr];
1326
1327         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1328                 goto close_json_markup;
1329
1330         json_markup_network(tab, OPEN_JSON_MARKUP);
1331         tab++;
1332
1333         xprintf0(tab, "\"net-tcp\": {"
1334                  "\"active\": %.2f, "
1335                  "\"passive\": %.2f, "
1336                  "\"iseg\": %.2f, "
1337                  "\"oseg\": %.2f}",
1338                  S_VALUE(sntp->ActiveOpens,  sntc->ActiveOpens,  itv),
1339                  S_VALUE(sntp->PassiveOpens, sntc->PassiveOpens, itv),
1340                  S_VALUE(sntp->InSegs,       sntc->InSegs,       itv),
1341                  S_VALUE(sntp->OutSegs,      sntc->OutSegs,      itv));
1342         tab--;
1343
1344 close_json_markup:
1345         if (CLOSE_MARKUP(a->options)) {
1346                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1347         }
1348 }
1349
1350 /*
1351  ***************************************************************************
1352  * Display TCP network errors statistics in JSON.
1353  *
1354  * IN:
1355  * @a           Activity structure with statistics.
1356  * @curr        Index in array for current sample statistics.
1357  * @tab         Indentation in XML output.
1358  * @itv         Interval of time in 1/100th of a second.
1359  ***************************************************************************
1360  */
1361 __print_funct_t json_print_net_etcp_stats(struct activity *a, int curr, int tab,
1362                                           unsigned long long itv)
1363 {
1364         struct stats_net_etcp
1365                 *snetc = (struct stats_net_etcp *) a->buf[curr],
1366                 *snetp = (struct stats_net_etcp *) a->buf[!curr];
1367
1368         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1369                 goto close_json_markup;
1370
1371         json_markup_network(tab, OPEN_JSON_MARKUP);
1372         tab++;
1373
1374         xprintf0(tab, "\"net-etcp\": {"
1375                  "\"atmptf\": %.2f, "
1376                  "\"estres\": %.2f, "
1377                  "\"retrans\": %.2f, "
1378                  "\"isegerr\": %.2f, "
1379                  "\"orsts\": %.2f}",
1380                  S_VALUE(snetp->AttemptFails, snetc->AttemptFails,  itv),
1381                  S_VALUE(snetp->EstabResets,  snetc->EstabResets,  itv),
1382                  S_VALUE(snetp->RetransSegs,  snetc->RetransSegs,  itv),
1383                  S_VALUE(snetp->InErrs,       snetc->InErrs,  itv),
1384                  S_VALUE(snetp->OutRsts,      snetc->OutRsts,  itv));
1385         tab--;
1386
1387 close_json_markup:
1388         if (CLOSE_MARKUP(a->options)) {
1389                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1390         }
1391 }
1392
1393 /*
1394  ***************************************************************************
1395  * Display UDP network statistics in JSON.
1396  *
1397  * IN:
1398  * @a           Activity structure with statistics.
1399  * @curr        Index in array for current sample statistics.
1400  * @tab         Indentation in output.
1401  * @itv         Interval of time in 1/100th of a second.
1402  ***************************************************************************
1403  */
1404 __print_funct_t json_print_net_udp_stats(struct activity *a, int curr, int tab,
1405                                          unsigned long long itv)
1406 {
1407         struct stats_net_udp
1408                 *snuc = (struct stats_net_udp *) a->buf[curr],
1409                 *snup = (struct stats_net_udp *) a->buf[!curr];
1410
1411         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1412                 goto close_json_markup;
1413
1414         json_markup_network(tab, OPEN_JSON_MARKUP);
1415         tab++;
1416
1417         xprintf0(tab, "\"net-udp\": {"
1418                  "\"idgm\": %.2f, "
1419                  "\"odgm\": %.2f, "
1420                  "\"noport\": %.2f, "
1421                  "\"idgmerr\": %.2f}",
1422                  S_VALUE(snup->InDatagrams,  snuc->InDatagrams,  itv),
1423                  S_VALUE(snup->OutDatagrams, snuc->OutDatagrams, itv),
1424                  S_VALUE(snup->NoPorts,      snuc->NoPorts,      itv),
1425                  S_VALUE(snup->InErrors,     snuc->InErrors,     itv));
1426         tab--;
1427
1428 close_json_markup:
1429         if (CLOSE_MARKUP(a->options)) {
1430                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1431         }
1432 }
1433
1434 /*
1435  ***************************************************************************
1436  * Display IPv6 network socket statistics in JSON.
1437  *
1438  * IN:
1439  * @a           Activity structure with statistics.
1440  * @curr        Index in array for current sample statistics.
1441  * @tab         Indentation in output.
1442  * @itv         Interval of time in 1/100th of a second.
1443  ***************************************************************************
1444  */
1445 __print_funct_t json_print_net_sock6_stats(struct activity *a, int curr, int tab,
1446                                            unsigned long long itv)
1447 {
1448         struct stats_net_sock6
1449                 *snsc = (struct stats_net_sock6 *) a->buf[curr];
1450
1451         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1452                 goto close_json_markup;
1453
1454         json_markup_network(tab, OPEN_JSON_MARKUP);
1455         tab++;
1456
1457         xprintf0(tab, "\"net-sock6\": {"
1458                  "\"tcp6sck\": %u, "
1459                  "\"udp6sck\": %u, "
1460                  "\"raw6sck\": %u, "
1461                  "\"ip6-frag\": %u}",
1462                  snsc->tcp6_inuse,
1463                  snsc->udp6_inuse,
1464                  snsc->raw6_inuse,
1465                  snsc->frag6_inuse);
1466         tab--;
1467
1468 close_json_markup:
1469         if (CLOSE_MARKUP(a->options)) {
1470                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1471         }
1472 }
1473
1474 /*
1475  ***************************************************************************
1476  * Display IPv6 network statistics in JSON.
1477  *
1478  * IN:
1479  * @a           Activity structure with statistics.
1480  * @curr        Index in array for current sample statistics.
1481  * @tab         Indentation in output.
1482  * @itv         Interval of time in 1/100th of a second.
1483  ***************************************************************************
1484  */
1485 __print_funct_t json_print_net_ip6_stats(struct activity *a, int curr, int tab,
1486                                          unsigned long long itv)
1487 {
1488         struct stats_net_ip6
1489                 *snic = (struct stats_net_ip6 *) a->buf[curr],
1490                 *snip = (struct stats_net_ip6 *) a->buf[!curr];
1491
1492         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1493                 goto close_json_markup;
1494
1495         json_markup_network(tab, OPEN_JSON_MARKUP);
1496         tab++;
1497
1498         xprintf0(tab, "\"net-ip6\": {"
1499                  "\"irec6\": %.2f, "
1500                  "\"fwddgm6\": %.2f, "
1501                  "\"idel6\": %.2f, "
1502                  "\"orq6\": %.2f, "
1503                  "\"asmrq6\": %.2f, "
1504                  "\"asmok6\": %.2f, "
1505                  "\"imcpck6\": %.2f, "
1506                  "\"omcpck6\": %.2f, "
1507                  "\"fragok6\": %.2f, "
1508                  "\"fragcr6\": %.2f}",
1509                  S_VALUE(snip->InReceives6,       snic->InReceives6,       itv),
1510                  S_VALUE(snip->OutForwDatagrams6, snic->OutForwDatagrams6, itv),
1511                  S_VALUE(snip->InDelivers6,       snic->InDelivers6,       itv),
1512                  S_VALUE(snip->OutRequests6,      snic->OutRequests6,      itv),
1513                  S_VALUE(snip->ReasmReqds6,       snic->ReasmReqds6,       itv),
1514                  S_VALUE(snip->ReasmOKs6,         snic->ReasmOKs6,         itv),
1515                  S_VALUE(snip->InMcastPkts6,      snic->InMcastPkts6,      itv),
1516                  S_VALUE(snip->OutMcastPkts6,     snic->OutMcastPkts6,     itv),
1517                  S_VALUE(snip->FragOKs6,          snic->FragOKs6,          itv),
1518                  S_VALUE(snip->FragCreates6,      snic->FragCreates6,      itv));
1519         tab--;
1520
1521 close_json_markup:
1522         if (CLOSE_MARKUP(a->options)) {
1523                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1524         }
1525 }
1526
1527 /*
1528  ***************************************************************************
1529  * Display IPv6 network errors statistics in JSON.
1530  *
1531  * IN:
1532  * @a           Activity structure with statistics.
1533  * @curr        Index in array for current sample statistics.
1534  * @tab         Indentation in output.
1535  * @itv         Interval of time in 1/100th of a second.
1536  ***************************************************************************
1537  */
1538 __print_funct_t json_print_net_eip6_stats(struct activity *a, int curr, int tab,
1539                                           unsigned long long itv)
1540 {
1541         struct stats_net_eip6
1542                 *sneic = (struct stats_net_eip6 *) a->buf[curr],
1543                 *sneip = (struct stats_net_eip6 *) a->buf[!curr];
1544
1545         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1546                 goto close_json_markup;
1547
1548         json_markup_network(tab, OPEN_JSON_MARKUP);
1549         tab++;
1550
1551         xprintf0(tab, "\"net-eip6\": {"
1552                  "\"ihdrer6\": %.2f, "
1553                  "\"iadrer6\": %.2f, "
1554                  "\"iukwnp6\": %.2f, "
1555                  "\"i2big6\": %.2f, "
1556                  "\"idisc6\": %.2f, "
1557                  "\"odisc6\": %.2f, "
1558                  "\"inort6\": %.2f, "
1559                  "\"onort6\": %.2f, "
1560                  "\"asmf6\": %.2f, "
1561                  "\"fragf6\": %.2f, "
1562                  "\"itrpck6\": %.2f}",
1563                  S_VALUE(sneip->InHdrErrors6,     sneic->InHdrErrors6,     itv),
1564                  S_VALUE(sneip->InAddrErrors6,    sneic->InAddrErrors6,    itv),
1565                  S_VALUE(sneip->InUnknownProtos6, sneic->InUnknownProtos6, itv),
1566                  S_VALUE(sneip->InTooBigErrors6,  sneic->InTooBigErrors6,  itv),
1567                  S_VALUE(sneip->InDiscards6,      sneic->InDiscards6,      itv),
1568                  S_VALUE(sneip->OutDiscards6,     sneic->OutDiscards6,     itv),
1569                  S_VALUE(sneip->InNoRoutes6,      sneic->InNoRoutes6,      itv),
1570                  S_VALUE(sneip->OutNoRoutes6,     sneic->OutNoRoutes6,     itv),
1571                  S_VALUE(sneip->ReasmFails6,      sneic->ReasmFails6,      itv),
1572                  S_VALUE(sneip->FragFails6,       sneic->FragFails6,       itv),
1573                  S_VALUE(sneip->InTruncatedPkts6, sneic->InTruncatedPkts6, itv));
1574         tab--;
1575
1576 close_json_markup:
1577         if (CLOSE_MARKUP(a->options)) {
1578                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1579         }
1580 }
1581
1582 /*
1583  ***************************************************************************
1584  * Display ICMPv6 network statistics in JSON.
1585  *
1586  * IN:
1587  * @a           Activity structure with statistics.
1588  * @curr        Index in array for current sample statistics.
1589  * @tab         Indentation in output.
1590  * @itv         Interval of time in 1/100th of a second.
1591  ***************************************************************************
1592  */
1593 __print_funct_t json_print_net_icmp6_stats(struct activity *a, int curr, int tab,
1594                                            unsigned long long itv)
1595 {
1596         struct stats_net_icmp6
1597                 *snic = (struct stats_net_icmp6 *) a->buf[curr],
1598                 *snip = (struct stats_net_icmp6 *) a->buf[!curr];
1599
1600         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1601                 goto close_json_markup;
1602
1603         json_markup_network(tab, OPEN_JSON_MARKUP);
1604         tab++;
1605
1606         xprintf0(tab, "\"net-icmp6\": {"
1607                  "\"imsg6\": %.2f, "
1608                  "\"omsg6\": %.2f, "
1609                  "\"iech6\": %.2f, "
1610                  "\"iechr6\": %.2f, "
1611                  "\"oechr6\": %.2f, "
1612                  "\"igmbq6\": %.2f, "
1613                  "\"igmbr6\": %.2f, "
1614                  "\"ogmbr6\": %.2f, "
1615                  "\"igmbrd6\": %.2f, "
1616                  "\"ogmbrd6\": %.2f, "
1617                  "\"irtsol6\": %.2f, "
1618                  "\"ortsol6\": %.2f, "
1619                  "\"irtad6\": %.2f, "
1620                  "\"inbsol6\": %.2f, "
1621                  "\"onbsol6\": %.2f, "
1622                  "\"inbad6\": %.2f, "
1623                  "\"onbad6\": %.2f}",
1624                  S_VALUE(snip->InMsgs6,                    snic->InMsgs6,                    itv),
1625                  S_VALUE(snip->OutMsgs6,                   snic->OutMsgs6,                   itv),
1626                  S_VALUE(snip->InEchos6,                   snic->InEchos6,                   itv),
1627                  S_VALUE(snip->InEchoReplies6,             snic->InEchoReplies6,             itv),
1628                  S_VALUE(snip->OutEchoReplies6,            snic->OutEchoReplies6,            itv),
1629                  S_VALUE(snip->InGroupMembQueries6,        snic->InGroupMembQueries6,        itv),
1630                  S_VALUE(snip->InGroupMembResponses6,      snic->InGroupMembResponses6,      itv),
1631                  S_VALUE(snip->OutGroupMembResponses6,     snic->OutGroupMembResponses6,     itv),
1632                  S_VALUE(snip->InGroupMembReductions6,     snic->InGroupMembReductions6,     itv),
1633                  S_VALUE(snip->OutGroupMembReductions6,    snic->OutGroupMembReductions6,    itv),
1634                  S_VALUE(snip->InRouterSolicits6,          snic->InRouterSolicits6,          itv),
1635                  S_VALUE(snip->OutRouterSolicits6,         snic->OutRouterSolicits6,         itv),
1636                  S_VALUE(snip->InRouterAdvertisements6,    snic->InRouterAdvertisements6,    itv),
1637                  S_VALUE(snip->InNeighborSolicits6,        snic->InNeighborSolicits6,        itv),
1638                  S_VALUE(snip->OutNeighborSolicits6,       snic->OutNeighborSolicits6,       itv),
1639                  S_VALUE(snip->InNeighborAdvertisements6,  snic->InNeighborAdvertisements6,  itv),
1640                  S_VALUE(snip->OutNeighborAdvertisements6, snic->OutNeighborAdvertisements6, itv));
1641         tab--;
1642
1643 close_json_markup:
1644         if (CLOSE_MARKUP(a->options)) {
1645                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1646         }
1647 }
1648
1649 /*
1650  ***************************************************************************
1651  * Display ICMPv6 error messages statistics in JSON.
1652  *
1653  * IN:
1654  * @a           Activity structure with statistics.
1655  * @curr        Index in array for current sample statistics.
1656  * @tab         Indentation in output.
1657  * @itv         Interval of time in 1/100th of a second.
1658  ***************************************************************************
1659  */
1660 __print_funct_t json_print_net_eicmp6_stats(struct activity *a, int curr, int tab,
1661                                             unsigned long long itv)
1662 {
1663         struct stats_net_eicmp6
1664                 *sneic = (struct stats_net_eicmp6 *) a->buf[curr],
1665                 *sneip = (struct stats_net_eicmp6 *) a->buf[!curr];
1666
1667         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1668                 goto close_json_markup;
1669
1670         json_markup_network(tab, OPEN_JSON_MARKUP);
1671         tab++;
1672
1673         xprintf0(tab, "\"net-eicmp6\": {"
1674                  "\"ierr6\": %.2f, "
1675                  "\"idtunr6\": %.2f, "
1676                  "\"odtunr6\": %.2f, "
1677                  "\"itmex6\": %.2f, "
1678                  "\"otmex6\": %.2f, "
1679                  "\"iprmpb6\": %.2f, "
1680                  "\"oprmpb6\": %.2f, "
1681                  "\"iredir6\": %.2f, "
1682                  "\"oredir6\": %.2f, "
1683                  "\"ipck2b6\": %.2f, "
1684                  "\"opck2b6\": %.2f}",
1685                  S_VALUE(sneip->InErrors6,        sneic->InErrors6,        itv),
1686                  S_VALUE(sneip->InDestUnreachs6,  sneic->InDestUnreachs6,  itv),
1687                  S_VALUE(sneip->OutDestUnreachs6, sneic->OutDestUnreachs6, itv),
1688                  S_VALUE(sneip->InTimeExcds6,     sneic->InTimeExcds6,     itv),
1689                  S_VALUE(sneip->OutTimeExcds6,    sneic->OutTimeExcds6,    itv),
1690                  S_VALUE(sneip->InParmProblems6,  sneic->InParmProblems6,  itv),
1691                  S_VALUE(sneip->OutParmProblems6, sneic->OutParmProblems6, itv),
1692                  S_VALUE(sneip->InRedirects6,     sneic->InRedirects6,     itv),
1693                  S_VALUE(sneip->OutRedirects6,    sneic->OutRedirects6,    itv),
1694                  S_VALUE(sneip->InPktTooBigs6,    sneic->InPktTooBigs6,    itv),
1695                  S_VALUE(sneip->OutPktTooBigs6,   sneic->OutPktTooBigs6,   itv));
1696         tab--;
1697
1698 close_json_markup:
1699         if (CLOSE_MARKUP(a->options)) {
1700                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1701         }
1702 }
1703
1704 /*
1705  ***************************************************************************
1706  * Display UDPv6 network statistics in JSON.
1707  *
1708  * IN:
1709  * @a           Activity structure with statistics.
1710  * @curr        Index in array for current sample statistics.
1711  * @tab         Indentation in output.
1712  * @itv         Interval of time in 1/100th of a second.
1713  ***************************************************************************
1714  */
1715 __print_funct_t json_print_net_udp6_stats(struct activity *a, int curr, int tab,
1716                                           unsigned long long itv)
1717 {
1718         struct stats_net_udp6
1719                 *snuc = (struct stats_net_udp6 *) a->buf[curr],
1720                 *snup = (struct stats_net_udp6 *) a->buf[!curr];
1721
1722         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1723                 goto close_json_markup;
1724
1725         json_markup_network(tab, OPEN_JSON_MARKUP);
1726         tab++;
1727
1728         xprintf0(tab, "\"net-udp6\": {"
1729                  "\"idgm6\": %.2f, "
1730                  "\"odgm6\": %.2f, "
1731                  "\"noport6\": %.2f, "
1732                  "\"idgmer6\": %.2f}",
1733                  S_VALUE(snup->InDatagrams6,  snuc->InDatagrams6,  itv),
1734                  S_VALUE(snup->OutDatagrams6, snuc->OutDatagrams6, itv),
1735                  S_VALUE(snup->NoPorts6,      snuc->NoPorts6,      itv),
1736                  S_VALUE(snup->InErrors6,     snuc->InErrors6,     itv));
1737         tab--;
1738
1739 close_json_markup:
1740         if (CLOSE_MARKUP(a->options)) {
1741                 json_markup_network(tab, CLOSE_JSON_MARKUP);
1742         }
1743 }
1744
1745 /*
1746  ***************************************************************************
1747  * Display CPU frequency statistics in JSON.
1748  *
1749  * IN:
1750  * @a           Activity structure with statistics.
1751  * @curr        Index in array for current sample statistics.
1752  * @tab         Indentation in output.
1753  * @itv         Interval of time in 1/100th of a second.
1754  ***************************************************************************
1755  */
1756 __print_funct_t json_print_pwr_cpufreq_stats(struct activity *a, int curr, int tab,
1757                                              unsigned long long itv)
1758 {
1759         int i;
1760         struct stats_pwr_cpufreq *spc;
1761         int sep = FALSE;
1762         char cpuno[8];
1763
1764         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1765                 goto close_json_markup;
1766
1767         json_markup_power_management(tab, OPEN_JSON_MARKUP);
1768         tab++;
1769
1770         xprintf(tab++, "\"cpu-frequency\": [");
1771
1772         for (i = 0; (i < a->nr) && (i < a->bitmap->b_size + 1); i++) {
1773
1774                 spc = (struct stats_pwr_cpufreq *) ((char *) a->buf[curr]  + i * a->msize);
1775
1776                 /* Should current CPU (including CPU "all") be displayed? */
1777                 if (a->bitmap->b_array[i >> 3] & (1 << (i & 0x07))) {
1778
1779                         /* Yes: Display it */
1780                         if (!i) {
1781                                 /* This is CPU "all" */
1782                                 strcpy(cpuno, "all");
1783                         }
1784                         else {
1785                                 sprintf(cpuno, "%d", i - 1);
1786                         }
1787
1788                         if (sep) {
1789                                 printf(",\n");
1790                         }
1791                         sep = TRUE;
1792
1793                         xprintf0(tab, "{\"number\": \"%s\", "
1794                                  "\"frequency\": %.2f}",
1795                                  cpuno,
1796                                  ((double) spc->cpufreq) / 100);
1797                 }
1798         }
1799
1800         printf("\n");
1801         xprintf0(--tab, "]");
1802         tab--;
1803
1804 close_json_markup:
1805         if (CLOSE_MARKUP(a->options)) {
1806                 json_markup_power_management(tab, CLOSE_JSON_MARKUP);
1807         }
1808 }
1809
1810 /*
1811  ***************************************************************************
1812  * Display fan statistics in JSON.
1813  *
1814  * IN:
1815  * @a           Activity structure with statistics.
1816  * @curr        Index in array for current sample statistics.
1817  * @tab         Indentation in output.
1818  * @itv         Interval of time in 1/100th of a second.
1819  ***************************************************************************
1820  */
1821 __print_funct_t json_print_pwr_fan_stats(struct activity *a, int curr, int tab,
1822                                          unsigned long long itv)
1823 {
1824         int i;
1825         struct stats_pwr_fan *spc;
1826         int sep = FALSE;
1827
1828         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1829                 goto close_json_markup;
1830
1831         json_markup_power_management(tab, OPEN_JSON_MARKUP);
1832         tab++;
1833
1834         xprintf(tab++, "\"fan-speed\": [");
1835
1836         for (i = 0; i < a->nr; i++) {
1837                 spc = (struct stats_pwr_fan *) ((char *) a->buf[curr]  + i * a->msize);
1838
1839                 if (sep) {
1840                         printf(",\n");
1841                 }
1842                 sep = TRUE;
1843
1844                 xprintf0(tab, "{\"number\": %d, "
1845                          "\"rpm\": %llu, "
1846                          "\"drpm\": %llu, "
1847                          "\"device\": \"%s\"}",
1848                          i + 1,
1849                          (unsigned long long) spc->rpm,
1850                          (unsigned long long) (spc->rpm - spc->rpm_min),
1851                          spc->device);
1852         }
1853
1854         printf("\n");
1855         xprintf0(--tab, "]");
1856         tab--;
1857
1858 close_json_markup:
1859         if (CLOSE_MARKUP(a->options)) {
1860                 json_markup_power_management(tab, CLOSE_JSON_MARKUP);
1861         }
1862 }
1863
1864 /*
1865  ***************************************************************************
1866  * Display temperature statistics in JSON.
1867  *
1868  * IN:
1869  * @a           Activity structure with statistics.
1870  * @curr        Index in array for current sample statistics.
1871  * @tab         Indentation in output.
1872  * @itv         Interval of time in 1/100th of a second.
1873  ***************************************************************************
1874  */
1875 __print_funct_t json_print_pwr_temp_stats(struct activity *a, int curr, int tab,
1876                                           unsigned long long itv)
1877 {
1878         int i;
1879         struct stats_pwr_temp *spc;
1880         int sep = FALSE;
1881
1882         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1883                 goto close_json_markup;
1884
1885         json_markup_power_management(tab, OPEN_JSON_MARKUP);
1886         tab++;
1887
1888         xprintf(tab++, "\"temperature\": [");
1889
1890         for (i = 0; i < a->nr; i++) {
1891                 spc = (struct stats_pwr_temp *) ((char *) a->buf[curr]  + i * a->msize);
1892
1893                 if (sep) {
1894                         printf(",\n");
1895                 }
1896                 sep = TRUE;
1897
1898                 xprintf0(tab, "{\"number\": %d, "
1899                          "\"degC\": %.2f, "
1900                          "\"percent-temp\": %.2f, "
1901                          "\"device\": \"%s\"}",
1902                          i + 1,
1903                          spc->temp,
1904                          (spc->temp_max - spc->temp_min) ?
1905                          (spc->temp - spc->temp_min) / (spc->temp_max - spc->temp_min) * 100 :
1906                          0.0,
1907                          spc->device);
1908         }
1909
1910         printf("\n");
1911         xprintf0(--tab, "]");
1912         tab--;
1913
1914 close_json_markup:
1915         if (CLOSE_MARKUP(a->options)) {
1916                 json_markup_power_management(tab, CLOSE_JSON_MARKUP);
1917         }
1918 }
1919
1920 /*
1921  ***************************************************************************
1922  * Display voltage inputs statistics in JSON.
1923  *
1924  * IN:
1925  * @a           Activity structure with statistics.
1926  * @curr        Index in array for current sample statistics.
1927  * @tab         Indentation in output.
1928  * @itv         Interval of time in 1/100th of a second.
1929  ***************************************************************************
1930  */
1931 __print_funct_t json_print_pwr_in_stats(struct activity *a, int curr, int tab,
1932                                         unsigned long long itv)
1933 {
1934         int i;
1935         struct stats_pwr_in *spc;
1936         int sep = FALSE;
1937
1938         if (!IS_SELECTED(a->options) || (a->nr <= 0))
1939                 goto close_json_markup;
1940
1941         json_markup_power_management(tab, OPEN_JSON_MARKUP);
1942         tab++;
1943
1944         xprintf(tab++, "\"voltage-input\": [");
1945
1946         for (i = 0; i < a->nr; i++) {
1947                 spc = (struct stats_pwr_in *) ((char *) a->buf[curr]  + i * a->msize);
1948
1949                 if (sep) {
1950                         printf(",\n");
1951                 }
1952                 sep = TRUE;
1953
1954                 xprintf0(tab, "{\"number\": %d, "
1955                          "\"inV\": %.2f, "
1956                          "\"percent-in\": %.2f, "
1957                          "\"device\": \"%s\"}",
1958                          i,
1959                          spc->in,
1960                          (spc->in_max - spc->in_min) ?
1961                          (spc->in - spc->in_min) / (spc->in_max - spc->in_min) * 100 :
1962                          0.0,
1963                          spc->device);
1964         }
1965
1966         printf("\n");
1967         xprintf0(--tab, "]");
1968         tab--;
1969
1970 close_json_markup:
1971         if (CLOSE_MARKUP(a->options)) {
1972                 json_markup_power_management(tab, CLOSE_JSON_MARKUP);
1973         }
1974 }
1975
1976 /*
1977  ***************************************************************************
1978  * Display huge pages statistics in JSON.
1979  *
1980  * IN:
1981  * @a           Activity structure with statistics.
1982  * @curr        Index in array for current sample statistics.
1983  * @tab         Indentation in output.
1984  * @itv         Interval of time in 1/100th of a second.
1985  ***************************************************************************
1986  */
1987 __print_funct_t json_print_huge_stats(struct activity *a, int curr, int tab,
1988                                       unsigned long long itv)
1989 {
1990         struct stats_huge
1991                 *smc = (struct stats_huge *) a->buf[curr];
1992
1993         xprintf0(tab, "\"hugepages\": {"
1994                  "\"hugfree\": %lu, "
1995                  "\"hugused\": %lu, "
1996                  "\"hugused-percent\": %.2f}",
1997                  smc->frhkb,
1998                  smc->tlhkb - smc->frhkb,
1999                  smc->tlhkb ?
2000                  SP_VALUE(smc->frhkb, smc->tlhkb, smc->tlhkb) :
2001                  0.0);
2002 }
2003
2004 /*
2005  ***************************************************************************
2006  * Display weighted CPU frequency statistics in JSON.
2007  *
2008  * IN:
2009  * @a           Activity structure with statistics.
2010  * @curr        Index in array for current sample statistics.
2011  * @tab         Indentation in output.
2012  * @itv         Interval of time in 1/100th of a second.
2013  ***************************************************************************
2014  */
2015 __print_funct_t json_print_pwr_wghfreq_stats(struct activity *a, int curr, int tab,
2016                                              unsigned long long itv)
2017 {
2018         int i, k;
2019         struct stats_pwr_wghfreq *spc, *spp, *spc_k, *spp_k;
2020         unsigned long long tis, tisfreq;
2021         int sep = FALSE;
2022         char cpuno[8];
2023
2024         if (!IS_SELECTED(a->options) || (a->nr <= 0))
2025                 goto close_json_markup;
2026
2027         json_markup_power_management(tab, OPEN_JSON_MARKUP);
2028         tab++;
2029
2030         xprintf(tab++, "\"cpu-weighted-frequency\": [");
2031
2032         for (i = 0; (i < a->nr) && (i < a->bitmap->b_size + 1); i++) {
2033
2034                 spc = (struct stats_pwr_wghfreq *) ((char *) a->buf[curr]  + i * a->msize * a->nr2);
2035                 spp = (struct stats_pwr_wghfreq *) ((char *) a->buf[!curr] + i * a->msize * a->nr2);
2036
2037                 /* Should current CPU (including CPU "all") be displayed? */
2038                 if (a->bitmap->b_array[i >> 3] & (1 << (i & 0x07))) {
2039
2040                         /* Yes... */
2041                         tisfreq = 0;
2042                         tis = 0;
2043
2044                         for (k = 0; k < a->nr2; k++) {
2045
2046                                 spc_k = (struct stats_pwr_wghfreq *) ((char *) spc + k * a->msize);
2047                                 if (!spc_k->freq)
2048                                         break;
2049                                 spp_k = (struct stats_pwr_wghfreq *) ((char *) spp + k * a->msize);
2050
2051                                 tisfreq += (spc_k->freq / 1000) *
2052                                            (spc_k->time_in_state - spp_k->time_in_state);
2053                                 tis     += (spc_k->time_in_state - spp_k->time_in_state);
2054                         }
2055
2056                         if (!i) {
2057                                 /* This is CPU "all" */
2058                                 strcpy(cpuno, "all");
2059                         }
2060                         else {
2061                                 sprintf(cpuno, "%d", i - 1);
2062                         }
2063
2064                         if (sep) {
2065                                 printf(",\n");
2066                         }
2067                         sep = TRUE;
2068
2069                         xprintf0(tab, "{\"number\": \"%s\", "
2070                                  "\"weighted-frequency\": %.2f}",
2071                                  cpuno,
2072                                  tis ? ((double) tisfreq) / tis : 0.0);
2073                 }
2074         }
2075
2076         printf("\n");
2077         xprintf0(--tab, "]");
2078         tab--;
2079
2080 close_json_markup:
2081         if (CLOSE_MARKUP(a->options)) {
2082                 json_markup_power_management(tab, CLOSE_JSON_MARKUP);
2083         }
2084 }
2085
2086 /*
2087  ***************************************************************************
2088  * Display USB devices statistics in JSON.
2089  *
2090  * IN:
2091  * @a           Activity structure with statistics.
2092  * @curr        Index in array for current sample statistics.
2093  * @tab         Indentation in output.
2094  * @itv         Interval of time in 1/100th of a second.
2095  ***************************************************************************
2096  */
2097 __print_funct_t json_print_pwr_usb_stats(struct activity *a, int curr, int tab,
2098                                          unsigned long long itv)
2099 {
2100         int i;
2101         struct stats_pwr_usb *suc;
2102         int sep = FALSE;
2103
2104         if (!IS_SELECTED(a->options) || (a->nr <= 0))
2105                 goto close_json_markup;
2106
2107         json_markup_power_management(tab, OPEN_JSON_MARKUP);
2108         tab++;
2109
2110         xprintf(tab++, "\"usb-devices\": [");
2111
2112         for (i = 0; i < a->nr; i++) {
2113                 suc = (struct stats_pwr_usb *) ((char *) a->buf[curr]  + i * a->msize);
2114
2115                 if (!suc->bus_nr)
2116                         /* Bus#0 doesn't exist: We are at the end of the list */
2117                         break;
2118
2119                 if (sep) {
2120                         printf(",\n");
2121                 }
2122                 sep = TRUE;
2123
2124                 xprintf0(tab, "{\"bus_number\": %d, "
2125                          "\"idvendor\": \"%x\", "
2126                          "\"idprod\": \"%x\", "
2127                          "\"maxpower\": %u, "
2128                          "\"manufact\": \"%s\", "
2129                          "\"product\": \"%s\"}",
2130                          suc->bus_nr,
2131                          suc->vendor_id,
2132                          suc->product_id,
2133                          suc->bmaxpower << 1,
2134                          suc->manufacturer,
2135                          suc->product);
2136         }
2137
2138         printf("\n");
2139         xprintf0(--tab, "]");
2140         tab--;
2141
2142 close_json_markup:
2143         if (CLOSE_MARKUP(a->options)) {
2144                 json_markup_power_management(tab, CLOSE_JSON_MARKUP);
2145         }
2146 }
2147
2148 /*
2149  ***************************************************************************
2150  * Display filesystems statistics in JSON.
2151  *
2152  * IN:
2153  * @a           Activity structure with statistics.
2154  * @curr        Index in array for current sample statistics.
2155  * @tab         Indentation in output.
2156  * @itv         Interval of time in 1/100th of a second.
2157  ***************************************************************************
2158  */
2159 __print_funct_t json_print_filesystem_stats(struct activity *a, int curr, int tab,
2160                                             unsigned long long itv)
2161 {
2162         int i;
2163         struct stats_filesystem *sfc;
2164         int sep = FALSE;
2165
2166         xprintf(tab++, "\"filesystems\": [");
2167
2168         for (i = 0; i < a->nr; i++) {
2169                 sfc = (struct stats_filesystem *) ((char *) a->buf[curr]  + i * a->msize);
2170
2171                 if (!sfc->f_blocks)
2172                         /* Size of filesystem is zero: We are at the end of the list */
2173                         break;
2174
2175                 if (sep) {
2176                         printf(",\n");
2177                 }
2178                 sep = TRUE;
2179
2180                 xprintf0(tab, "{\"%s\": \"%s\", "
2181                          "\"MBfsfree\": %.0f, "
2182                          "\"MBfsused\": %.0f, "
2183                          "\"%%fsused\": %.2f, "
2184                          "\"%%ufsused\": %.2f, "
2185                          "\"Ifree\": %llu, "
2186                          "\"Iused\": %llu, "
2187                          "\"%%Iused\": %.2f}",
2188                          DISPLAY_MOUNT(a->opt_flags) ? "mountpoint" : "filesystem",
2189                          DISPLAY_MOUNT(a->opt_flags) ? sfc->mountp : sfc->fs_name,
2190                          (double) sfc->f_bfree / 1024 / 1024,
2191                          (double) (sfc->f_blocks - sfc->f_bfree) / 1024 / 1024,
2192                          sfc->f_blocks ? SP_VALUE(sfc->f_bfree, sfc->f_blocks, sfc->f_blocks)
2193                                      : 0.0,
2194                          sfc->f_blocks ? SP_VALUE(sfc->f_bavail, sfc->f_blocks, sfc->f_blocks)
2195                                      : 0.0,
2196                          sfc->f_ffree,
2197                          sfc->f_files - sfc->f_ffree,
2198                          sfc->f_files ? SP_VALUE(sfc->f_ffree, sfc->f_files, sfc->f_files)
2199                                     : 0.0);
2200         }
2201
2202         printf("\n");
2203         xprintf0(--tab, "]");
2204 }
2205
2206 /*
2207  ***************************************************************************
2208  * Display Fibre Channel HBA statistics in JSON.
2209  *
2210  * IN:
2211  * @a           Activity structure with statistics.
2212  * @curr        Index in array for current sample statistics.
2213  * @tab         Indentation in output.
2214  * @itv         Interval of time in 1/100th of a second.
2215  ***************************************************************************
2216  */
2217 __print_funct_t json_print_fchost_stats(struct activity *a, int curr, int tab,
2218                                         unsigned long long itv)
2219 {
2220         int i;
2221         struct stats_fchost *sfcc, *sfcp;
2222         int sep = FALSE;
2223
2224         if (!IS_SELECTED(a->options) || (a->nr <= 0))
2225                 goto close_json_markup;
2226
2227         json_markup_network(tab, OPEN_JSON_MARKUP);
2228         tab++;
2229
2230         xprintf(tab++, "\"fchosts\": [");
2231
2232         for (i = 0; i < a->nr; i++) {
2233                 sfcc = (struct stats_fchost *) ((char *) a->buf[curr]  + i * a->msize);
2234                 sfcp = (struct stats_fchost *) ((char *) a->buf[!curr]  + i * a->msize);
2235
2236                 if (!sfcc->fchost_name[0])
2237                         /* We are at the end of the list */
2238                         break;
2239
2240                 if (sep)
2241                         printf(",\n");
2242
2243                 sep = TRUE;
2244
2245                 xprintf0(tab, "{\"fchost\": \"%s\", "
2246                          "\"fch_rxf\": %.2f, "
2247                          "\"fch_txf\": %.2f, "
2248                          "\"fch_rxw\": %.2f, "
2249                          "\"fch_txw\": %.2f}",
2250                          sfcc->fchost_name,
2251                          S_VALUE(sfcp->f_rxframes, sfcc->f_rxframes, itv),
2252                          S_VALUE(sfcp->f_txframes, sfcc->f_txframes, itv),
2253                          S_VALUE(sfcp->f_rxwords,  sfcc->f_rxwords,  itv),
2254                          S_VALUE(sfcp->f_txwords,  sfcc->f_txwords,  itv));
2255         }
2256
2257         printf("\n");
2258         xprintf0(--tab, "]");
2259
2260         tab --;
2261
2262 close_json_markup:
2263         if (CLOSE_MARKUP(a->options)) {
2264                 json_markup_network(tab, CLOSE_JSON_MARKUP);
2265         }
2266 }
2267
2268 /*
2269  ***************************************************************************
2270  * Display softnet statistics in JSON.
2271  *
2272  * IN:
2273  * @a           Activity structure with statistics.
2274  * @curr        Index in array for current sample statistics.
2275  * @tab         Indentation in output.
2276  * @itv         Interval of time in 1/100th of a second.
2277  ***************************************************************************
2278  */
2279 __print_funct_t json_print_softnet_stats(struct activity *a, int curr, int tab,
2280                                          unsigned long long itv)
2281 {
2282         int i;
2283         struct stats_softnet *ssnc, *ssnp;
2284         int sep = FALSE;
2285         char cpuno[8];
2286
2287         if (!IS_SELECTED(a->options) || (a->nr <= 0))
2288                 goto close_json_markup;
2289
2290         json_markup_network(tab, OPEN_JSON_MARKUP);
2291         tab++;
2292
2293         xprintf(tab++, "\"softnet\": [");
2294
2295         for (i = 0; (i < a->nr) && (i < a->bitmap->b_size + 1); i++) {
2296
2297                 /*
2298                  * The size of a->buf[...] CPU structure may be different from the default
2299                  * sizeof(struct stats_pwr_cpufreq) value if data have been read from a file!
2300                  * That's why we don't use a syntax like:
2301                  * ssnc = (struct stats_softnet *) a->buf[...] + i;
2302                  */
2303                 ssnc = (struct stats_softnet *) ((char *) a->buf[curr] + i * a->msize);
2304                 ssnp = (struct stats_softnet *) ((char *) a->buf[!curr] + i * a->msize);
2305
2306                 /*
2307                  * Note: a->nr is in [1, NR_CPUS + 1].
2308                  * Bitmap size is provided for (NR_CPUS + 1) CPUs.
2309                  * Anyway, NR_CPUS may vary between the version of sysstat
2310                  * used by sadc to create a file, and the version of sysstat
2311                  * used by sar to read it...
2312                  */
2313
2314                 /* Should current CPU (including CPU "all") be displayed? */
2315                 if (!(a->bitmap->b_array[i >> 3] & (1 << (i & 0x07))))
2316                         /* No */
2317                         continue;
2318
2319                 /* Yes: Display current CPU stats */
2320
2321                 if (sep) {
2322                         printf(",\n");
2323                 }
2324                 sep = TRUE;
2325
2326                 if (!i) {
2327                         /* This is CPU "all" */
2328                         strcpy(cpuno, "all");
2329                 }
2330                 else {
2331                         sprintf(cpuno, "%d", i - 1);
2332                 }
2333
2334                 xprintf0(tab, "{\"cpu\": \"%s\", "
2335                          "\"total\": %.2f, "
2336                          "\"dropd\": %.2f, "
2337                          "\"squeezd\": %.2f, "
2338                          "\"rx_rps\": %.2f, "
2339                          "\"flw_lim\": %.2f}",
2340                          cpuno,
2341                          S_VALUE(ssnp->processed,    ssnc->processed,    itv),
2342                          S_VALUE(ssnp->dropped,      ssnc->dropped,      itv),
2343                          S_VALUE(ssnp->time_squeeze, ssnc->time_squeeze, itv),
2344                          S_VALUE(ssnp->received_rps, ssnc->received_rps, itv),
2345                          S_VALUE(ssnp->flow_limit,   ssnc->flow_limit,   itv));
2346         }
2347
2348         printf("\n");
2349         xprintf0(--tab, "]");
2350
2351         tab --;
2352
2353 close_json_markup:
2354         if (CLOSE_MARKUP(a->options)) {
2355                 json_markup_network(tab, CLOSE_JSON_MARKUP);
2356         }
2357 }