File: nfsmeter.cc

package info (click to toggle)
xosview 1.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: cpp: 11,975; makefile: 154; ansic: 32; awk: 13; sh: 8
file content (207 lines) | stat: -rw-r--r-- 5,287 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
//  Copyright (c) 1994, 1995, 2002, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
//
//  Modifications to support dynamic addresses by:
//    Michael N. Lipp (mnl@dtro.e-technik.th-darmstadt.de)
//
//  This file may be distributed under terms of the GPL
//

#include "nfsmeter.h"
#include <string.h>
#include <stdio.h>
#include <fstream>
// #include <iostream>

#ifndef MAX
#define MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#endif

static const char *NFSSVCSTAT = "/proc/net/rpc/nfsd";
static const char * NFSCLTSTAT = "/proc/net/rpc/nfs";

NFSMeter::NFSMeter(XOSView *parent, const char *name, int nfields,
		const char *fields, const char *statfile)
  : FieldMeterGraph( parent, nfields, name, fields ){
	_statfile = statfile;
	_statname = name;
}

NFSMeter::~NFSMeter( void ){
}

void NFSMeter::checkResources( void ){
  FieldMeterGraph::checkResources();
}

NFSDStats::NFSDStats(XOSView *parent)
  : NFSMeter(parent, "NFSD", 4, "BAD/UDP/TCP/IDLE", NFSSVCSTAT ){
	starttimer();
}

NFSDStats::~NFSDStats( void ) {
}

void NFSDStats::checkResources( void ){
  NFSMeter::checkResources();

  setfieldcolor( 0, parent_->getResource( "NFSDStatBadCallsColor" ) );
  setfieldcolor( 1, parent_->getResource( "NFSDStatUDPColor" ) );
  setfieldcolor( 2, parent_->getResource( "NFSDStatTCPColor" ) );
  setfieldcolor( 3, parent_->getResource( "NFSDStatIdleColor" ) );

  useGraph_ = parent_->isResourceTrue( "NFSDStatGraph" );
  dodecay_ = parent_->isResourceTrue( "NFSDStatDecay" );
  SetUsedFormat (parent_->getResource("NFSDStatUsedFormat"));
  //useGraph_ = 1;
  //dodecay_ = 1;
  //SetUsedFormat ("autoscale");
  //SetUsedFormat ("percent");
}
void NFSDStats::checkevent(void)
{
	char buf[4096], name[64];
	unsigned long netcnt, netudpcnt, nettcpcnt, nettcpconn;
	unsigned long calls, badcalls;
	int found;

    std::ifstream ifs(_statfile);

    if (!ifs) {
        // cerr <<"Can not open file : " <<_statfile <<endl;
        // parent_->done(1);
        return;
	}

	fields_[0] = fields_[1] = fields_[2] = 0;  // network activity
    stoptimer();

	name[0] = '\0';
	found = 0;
	while (!ifs.eof() && found != 2) {
		ifs.getline(buf, 4096, '\n');
		if (strncmp("net", buf, strlen("net")) == 0) {
			sscanf(buf, "%s %lu %lu %lu %lu\n", name,
				&netcnt, &netudpcnt, &nettcpcnt, &nettcpconn);
			found++;
		}
		if (strncmp("rpc", buf, strlen("rpc")) == 0) {
			sscanf(buf, "%s %lu %lu\n", name, &calls, &badcalls);
			found++;
		}
	}

    float t = 1000000.0 / usecs();

    if (t < 0)
        t = 0.1;

    maxpackets_ = MAX(netcnt, calls) - _lastNetCnt;
    if (maxpackets_ == 0) {
        maxpackets_ = netcnt;
    } else {
        fields_[0] = (badcalls - _lastBad) * t;
        fields_[1] = (netudpcnt - _lastUdp) * t;
        fields_[2] = (nettcpcnt - _lastTcp) * t;
    }

    total_ = fields_[0] + fields_[1] + fields_[2];
    if (total_ > maxpackets_)
        fields_[3] = 0;
    else {
        total_ = maxpackets_;
        fields_[3] = total_ - fields_[0] - fields_[1] - fields_[2];
	}

    if (total_)
        setUsed(fields_[0] + fields_[1] + fields_[2], total_);

    starttimer();
    drawfields();

	_lastNetCnt = MAX(netcnt, calls);
    _lastTcp = nettcpcnt;
    _lastUdp = netudpcnt;
    _lastBad = badcalls;
}

NFSStats::NFSStats(XOSView *parent)
  : NFSMeter(parent, "NFS", 4, "RETRY/AUTH/CALL/IDLE", NFSCLTSTAT ){
	starttimer();
}

NFSStats::~NFSStats( void ) {
}

void NFSStats::checkResources( void ){
  NFSMeter::checkResources();

  setfieldcolor( 0, parent_->getResource( "NFSStatReTransColor" ) );
  setfieldcolor( 1, parent_->getResource( "NFSStatAuthRefrshColor" ) );
  setfieldcolor( 2, parent_->getResource( "NFSStatCallsColor" ) );
  setfieldcolor( 3, parent_->getResource( "NFSStatIdleColor" ) );

  useGraph_ = parent_->isResourceTrue( "NFSStatGraph" );
  dodecay_ = parent_->isResourceTrue( "NFSStatDecay" );
  SetUsedFormat (parent_->getResource("NFSStatUsedFormat"));
  //SetUsedFormat ("autoscale");
  //SetUsedFormat ("percent");
}

void NFSStats::checkevent(void)
{
	char buf[4096], name[64];
	unsigned long calls = 0, retrns = 0, authrefresh = 0, maxpackets_;

    std::ifstream ifs(_statfile);

    if (!ifs) {
        // cerr <<"Can not open file : " <<_statfile <<endl;
        // parent_->done(1);
        return;
	}

	fields_[0] = fields_[1] = fields_[2] = 0;
    stoptimer();

	name[0] = '\0';
	while (!ifs.eof()) {
		ifs.getline(buf, 4096, '\n');
		if (strncmp("rpc", buf, strlen("rpc")))
			continue;
		sscanf(buf, "%s %lu %lu %lu\n", name, &calls, &retrns, &authrefresh);
		break;
	}

    float t = 1000000.0 / usecs();

    if (t < 0)
        t = 0.1;

    maxpackets_ = calls - _lastcalls;
    if (maxpackets_ == 0) {
        maxpackets_ = calls;
    } else {
        fields_[2] = (calls - _lastcalls) * t;
        fields_[1] = (authrefresh - _lastauthrefresh) * t;
        fields_[0] = (retrns - _lastretrns) * t;
    }

    total_ = fields_[0] + fields_[1] + fields_[2];
    if (total_ > maxpackets_)
        fields_[3] = 0;
    else {
        total_ = maxpackets_;
        fields_[3] = total_ - fields_[2] - fields_[1] - fields_[0];
	}

    if (total_)
        setUsed(fields_[0] + fields_[1] + fields_[2], total_);

    starttimer();
    drawfields();

	_lastcalls = calls;
	_lastretrns = retrns;
	_lastauthrefresh = authrefresh;
}