Skip to content

Commit

Permalink
Add network namespace attributes for NET
Browse files Browse the repository at this point in the history
The network devices, like eth2, are settled in another network
namespace via `ip netns add` in some scenarios in the production
environment. In this case, we have to switch to that new netns
to get network related statistics, e.g. reading from /proc/net/dev,
/proc/net/snmp, /proc/net/snmp6.

This patch firstly adds the needed attributes, like 'netns', for
collecting and displaying network namespace statistics. Then the
following patches will do the real implementation.

To pass the compiling, also update for atopsar, parsable and json.

Also format some lines according to astyle.

Signed-off-by: Fei Li <[email protected]>
  • Loading branch information
ShirleyFei committed Feb 21, 2023
1 parent c44c37a commit ab7ee8c
Show file tree
Hide file tree
Showing 10 changed files with 1,146 additions and 930 deletions.
527 changes: 306 additions & 221 deletions atopsar.c

Large diffs are not rendered by default.

590 changes: 311 additions & 279 deletions deviate.c

Large diffs are not rendered by default.

128 changes: 71 additions & 57 deletions json.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,68 +726,82 @@ static void json_print_NFS(char *hp, struct sstat *ss, struct tstat *ps, int nac

static void json_print_NET(char *hp, struct sstat *ss, struct tstat *ps, int nact)
{
register int i;
register int i, nr;

printf(", \"NET_GENERAL\": {"
"\"rpacketsTCP\": %lld, "
"\"spacketsTCP\": %lld, "
"\"activeOpensTCP\": %lld, "
"\"passiveOpensTCP\": %lld, "
"\"retransSegsTCP\": %lld, "
"\"rpacketsUDP\": %lld, "
"\"spacketsUDP\": %lld, "
"\"rpacketsIP\": %lld, "
"\"spacketsIP\": %lld, "
"\"dpacketsIP\": %lld, "
"\"fpacketsIP\": %lld, "
"\"icmpi\" : %lld, "
"\"icmpo\" : %lld}",
ss->net.tcp.InSegs,
ss->net.tcp.OutSegs,
ss->net.tcp.ActiveOpens,
ss->net.tcp.PassiveOpens,
ss->net.tcp.RetransSegs,
ss->net.udpv4.InDatagrams +
ss->net.udpv6.Udp6InDatagrams,
ss->net.udpv4.OutDatagrams +
ss->net.udpv6.Udp6OutDatagrams,
ss->net.ipv4.InReceives +
ss->net.ipv6.Ip6InReceives,
ss->net.ipv4.OutRequests +
ss->net.ipv6.Ip6OutRequests,
ss->net.ipv4.InDelivers +
ss->net.ipv6.Ip6InDelivers,
ss->net.ipv4.ForwDatagrams +
ss->net.ipv6.Ip6OutForwDatagrams,
ss->net.icmpv4.InMsgs +
ss->net.icmpv6.Icmp6InMsgs,
ss->net.icmpv4.OutMsgs +
ss->net.icmpv6.Icmp6OutMsgs);
printf(", \"NET_GENERAL\": [");
for (nr = 0; nr < ss->net.nrnetns; nr++) {
if (nr > 0) {
printf(", ");
}
printf("{\"netns\": \"%.19s\", "
"\"rpacketsTCP\": %lld, "
"\"spacketsTCP\": %lld, "
"\"activeOpensTCP\": %lld, "
"\"passiveOpensTCP\": %lld, "
"\"retransSegsTCP\": %lld, "
"\"rpacketsUDP\": %lld, "
"\"spacketsUDP\": %lld, "
"\"rpacketsIP\": %lld, "
"\"spacketsIP\": %lld, "
"\"dpacketsIP\": %lld, "
"\"fpacketsIP\": %lld, "
"\"icmpi\" : %lld, "
"\"icmpo\" : %lld}",
ss->net.netns[nr].nsname,
ss->net.netns[nr].tcp.InSegs,
ss->net.netns[nr].tcp.OutSegs,
ss->net.netns[nr].tcp.ActiveOpens,
ss->net.netns[nr].tcp.PassiveOpens,
ss->net.netns[nr].tcp.RetransSegs,
ss->net.netns[nr].udpv4.InDatagrams +
ss->net.netns[nr].udpv6.Udp6InDatagrams,
ss->net.netns[nr].udpv4.OutDatagrams +
ss->net.netns[nr].udpv6.Udp6OutDatagrams,
ss->net.netns[nr].ipv4.InReceives +
ss->net.netns[nr].ipv6.Ip6InReceives,
ss->net.netns[nr].ipv4.OutRequests +
ss->net.netns[nr].ipv6.Ip6OutRequests,
ss->net.netns[nr].ipv4.InDelivers +
ss->net.netns[nr].ipv6.Ip6InDelivers,
ss->net.netns[nr].ipv4.ForwDatagrams +
ss->net.netns[nr].ipv6.Ip6OutForwDatagrams,
ss->net.netns[nr].icmpv4.InMsgs +
ss->net.netns[nr].icmpv6.Icmp6InMsgs,
ss->net.netns[nr].icmpv4.OutMsgs +
ss->net.netns[nr].icmpv6.Icmp6OutMsgs);
}

printf(", %s: [", hp);
printf(", %s: [", hp);

for (i = 0; ss->intf.intf[i].name[0]; i++) {
if (i > 0) {
for (nr = 0; nr < ss->intf.nrintfns; nr++) {
if (nr > 0) {
printf(", ");
}
printf("{\"name\": \"%.19s\", "
"\"rpack\": %lld, "
"\"rbyte\": %lld, "
"\"rerrs\": %lld, "
"\"spack\": %lld, "
"\"sbyte\": %lld, "
"\"serrs\": %lld, "
"\"speed\": \"%ld\", "
"\"duplex\": %d}",
ss->intf.intf[i].name,
ss->intf.intf[i].rpack,
ss->intf.intf[i].rbyte,
ss->intf.intf[i].rerrs,
ss->intf.intf[i].spack,
ss->intf.intf[i].sbyte,
ss->intf.intf[i].serrs,
ss->intf.intf[i].speed,
ss->intf.intf[i].duplex);
for (i = 0; ss->intf.intfns[nr].intf[i].name[0]; i++) {
if (i > 0) {
printf(", ");
}
printf("{\"name\": \"%.19s\", "
"\"netns\": \"%.12s\", "
"\"rpack\": %lld, "
"\"rbyte\": %lld, "
"\"rerrs\": %lld, "
"\"spack\": %lld, "
"\"sbyte\": %lld, "
"\"serrs\": %lld, "
"\"speed\": \"%ld\", "
"\"duplex\": %d}",
ss->intf.intfns[nr].intf[i].name,
ss->intf.intfns[nr].nsname,
ss->intf.intfns[nr].intf[i].rpack,
ss->intf.intfns[nr].intf[i].rbyte,
ss->intf.intfns[nr].intf[i].rerrs,
ss->intf.intfns[nr].intf[i].spack,
ss->intf.intfns[nr].intf[i].sbyte,
ss->intf.intfns[nr].intf[i].serrs,
ss->intf.intfns[nr].intf[i].speed,
ss->intf.intfns[nr].intf[i].duplex);
}
}

printf("]");
Expand Down
82 changes: 43 additions & 39 deletions parseable.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,47 +598,51 @@ print_NFS(char *hp, struct sstat *ss, struct tstat *ps, int nact)
void
print_NET(char *hp, struct sstat *ss, struct tstat *ps, int nact)
{
register int i;
register int i, nr;

printf( "%s %s %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld\n",
hp,
for (nr = 0; nr < ss->net.nrnetns; nr++) {
printf( "%s %s %s %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld\n",
hp, ss->net.netns[nr].nsname,
"upper",
ss->net.tcp.InSegs,
ss->net.tcp.OutSegs,
ss->net.udpv4.InDatagrams +
ss->net.udpv6.Udp6InDatagrams,
ss->net.udpv4.OutDatagrams +
ss->net.udpv6.Udp6OutDatagrams,
ss->net.ipv4.InReceives +
ss->net.ipv6.Ip6InReceives,
ss->net.ipv4.OutRequests +
ss->net.ipv6.Ip6OutRequests,
ss->net.ipv4.InDelivers +
ss->net.ipv6.Ip6InDelivers,
ss->net.ipv4.ForwDatagrams +
ss->net.ipv6.Ip6OutForwDatagrams,
ss->net.udpv4.InErrors +
ss->net.udpv6.Udp6InErrors,
ss->net.udpv4.NoPorts +
ss->net.udpv6.Udp6NoPorts,
ss->net.tcp.ActiveOpens,
ss->net.tcp.PassiveOpens,
ss->net.tcp.CurrEstab,
ss->net.tcp.RetransSegs,
ss->net.tcp.InErrs,
ss->net.tcp.OutRsts);

for (i=0; ss->intf.intf[i].name[0]; i++)
{
printf( "%s %s %lld %lld %lld %lld %ld %d\n",
hp,
ss->intf.intf[i].name,
ss->intf.intf[i].rpack,
ss->intf.intf[i].rbyte,
ss->intf.intf[i].spack,
ss->intf.intf[i].sbyte,
ss->intf.intf[i].speed,
ss->intf.intf[i].duplex);
ss->net.netns[nr].tcp.InSegs,
ss->net.netns[nr].tcp.OutSegs,
ss->net.netns[nr].udpv4.InDatagrams +
ss->net.netns[nr].udpv6.Udp6InDatagrams,
ss->net.netns[nr].udpv4.OutDatagrams +
ss->net.netns[nr].udpv6.Udp6OutDatagrams,
ss->net.netns[nr].ipv4.InReceives +
ss->net.netns[nr].ipv6.Ip6InReceives,
ss->net.netns[nr].ipv4.OutRequests +
ss->net.netns[nr].ipv6.Ip6OutRequests,
ss->net.netns[nr].ipv4.InDelivers +
ss->net.netns[nr].ipv6.Ip6InDelivers,
ss->net.netns[nr].ipv4.ForwDatagrams +
ss->net.netns[nr].ipv6.Ip6OutForwDatagrams,
ss->net.netns[nr].udpv4.InErrors +
ss->net.netns[nr].udpv6.Udp6InErrors,
ss->net.netns[nr].udpv4.NoPorts +
ss->net.netns[nr].udpv6.Udp6NoPorts,
ss->net.netns[nr].tcp.ActiveOpens,
ss->net.netns[nr].tcp.PassiveOpens,
ss->net.netns[nr].tcp.CurrEstab,
ss->net.netns[nr].tcp.RetransSegs,
ss->net.netns[nr].tcp.InErrs,
ss->net.netns[nr].tcp.OutRsts);
}

for (nr = 0; nr < ss->intf.nrintfns; nr++) {
for (i=0; ss->intf.intfns[nr].intf[i].name[0]; i++)
{
printf( "%s %s %s %lld %lld %lld %lld %ld %d\n",
hp, ss->intf.intfns[nr].nsname,
ss->intf.intfns[nr].intf[i].name,
ss->intf.intfns[nr].intf[i].rpack,
ss->intf.intfns[nr].intf[i].rbyte,
ss->intf.intfns[nr].intf[i].spack,
ss->intf.intfns[nr].intf[i].sbyte,
ss->intf.intfns[nr].intf[i].speed,
ss->intf.intfns[nr].intf[i].duplex);
}
}
}

Expand Down
66 changes: 35 additions & 31 deletions photosyst.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,23 +1066,23 @@ photosyst(struct sstat *si)
"%15s %lld %lld %lld %lld %lld %lld %lld "
"%lld %lld %lld %lld %lld %lld %lld %lld "
"%lld\n",
si->intf.intf[i].name,
&(si->intf.intf[i].rbyte),
&(si->intf.intf[i].rpack),
&(si->intf.intf[i].rerrs),
&(si->intf.intf[i].rdrop),
&(si->intf.intf[i].rfifo),
&(si->intf.intf[i].rframe),
&(si->intf.intf[i].rcompr),
&(si->intf.intf[i].rmultic),
&(si->intf.intf[i].sbyte),
&(si->intf.intf[i].spack),
&(si->intf.intf[i].serrs),
&(si->intf.intf[i].sdrop),
&(si->intf.intf[i].sfifo),
&(si->intf.intf[i].scollis),
&(si->intf.intf[i].scarrier),
&(si->intf.intf[i].scompr));
si->intf.intfns[0].intf[i].name,
&(si->intf.intfns[0].intf[i].rbyte),
&(si->intf.intfns[0].intf[i].rpack),
&(si->intf.intfns[0].intf[i].rerrs),
&(si->intf.intfns[0].intf[i].rdrop),
&(si->intf.intfns[0].intf[i].rfifo),
&(si->intf.intfns[0].intf[i].rframe),
&(si->intf.intfns[0].intf[i].rcompr),
&(si->intf.intfns[0].intf[i].rmultic),
&(si->intf.intfns[0].intf[i].sbyte),
&(si->intf.intfns[0].intf[i].spack),
&(si->intf.intfns[0].intf[i].serrs),
&(si->intf.intfns[0].intf[i].sdrop),
&(si->intf.intfns[0].intf[i].sfifo),
&(si->intf.intfns[0].intf[i].scollis),
&(si->intf.intfns[0].intf[i].scarrier),
&(si->intf.intfns[0].intf[i].scompr));

/*
** skip header line and lines without stats
Expand All @@ -1096,7 +1096,7 @@ photosyst(struct sstat *si)
** because the total number of interfaces
** exceeds the maximum supported by atop (MAXINTF)
*/
strcpy(ifprop.name, si->intf.intf[i].name);
strcpy(ifprop.name, si->intf.intfns[0].intf[i].name);

if (!getifprop(&ifprop))
continue;
Expand All @@ -1115,8 +1115,10 @@ photosyst(struct sstat *si)
break;
}

si->intf.intf[i].name[0] = '\0'; /* set terminator for table */
si->intf.nrintf = i;
si->intf.intfns[0].intf[i].name[0] = '\0'; /* set terminator for table */
si->intf.intfns[0].nrintf = i;
strcpy(si->intf.intfns[0].nsname, "init");
si->intf.nrintfns = 1;

fclose(fp);
}
Expand Down Expand Up @@ -1151,34 +1153,36 @@ photosyst(struct sstat *si)

if ( strcmp("Ip:", nam) == 0)
{
memcpy(&si->net.ipv4, cnts,
sizeof si->net.ipv4);
memcpy(&si->net.netns[0].ipv4, cnts,
sizeof si->net.netns[0].ipv4);
continue;
}

if ( strcmp("Icmp:", nam) == 0)
{
memcpy(&si->net.icmpv4, cnts,
sizeof si->net.icmpv4);
memcpy(&si->net.netns[0].icmpv4, cnts,
sizeof si->net.netns[0].icmpv4);
continue;
}

if ( strcmp("Tcp:", nam) == 0)
{
memcpy(&si->net.tcp, cnts,
sizeof si->net.tcp);
memcpy(&si->net.netns[0].tcp, cnts,
sizeof si->net.netns[0].tcp);
continue;
}

if ( strcmp("Udp:", nam) == 0)
{
memcpy(&si->net.udpv4, cnts,
sizeof si->net.udpv4);
memcpy(&si->net.netns[0].udpv4, cnts,
sizeof si->net.netns[0].udpv4);
continue;
}
}

fclose(fp);
strcpy(si->net.netns[0].nsname, "init");
si->net.nrnetns = 1;
}

/*
Expand Down Expand Up @@ -1221,9 +1225,9 @@ photosyst(struct sstat *si)
cur = 0;
}

memcpy(&si->net.ipv6, &ipv6_tmp, sizeof ipv6_tmp);
memcpy(&si->net.icmpv6, &icmpv6_tmp, sizeof icmpv6_tmp);
memcpy(&si->net.udpv6, &udpv6_tmp, sizeof udpv6_tmp);
memcpy(&si->net.netns[0].ipv6, &ipv6_tmp, sizeof ipv6_tmp);
memcpy(&si->net.netns[0].icmpv6, &icmpv6_tmp, sizeof icmpv6_tmp);
memcpy(&si->net.netns[0].udpv6, &udpv6_tmp, sizeof udpv6_tmp);

fclose(fp);
}
Expand Down
Loading

0 comments on commit ab7ee8c

Please sign in to comment.