Skip to content

Commit 9cccefc

Browse files
committed
Move proc net stuff
1 parent fad48e5 commit 9cccefc

File tree

1 file changed

+110
-108
lines changed

1 file changed

+110
-108
lines changed

linuxpy/proc.py

Lines changed: 110 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
MODULES_PATH: Path = PROC_PATH / "modules"
1616
STAT_PATH: Path = PROC_PATH / "stat"
1717
NET_PATH: Path = PROC_PATH / "net"
18-
DEV_PATH: Path = NET_PATH / "dev"
18+
NET_DEV_PATH: Path = NET_PATH / "dev"
1919
WIRELESS_PATH: Path = NET_PATH / "wireless"
2020
NETSTAT_PATH = NET_PATH / "netstat"
2121
SNMP_PATH = NET_PATH / "snmp"
@@ -134,113 +134,115 @@ def stat():
134134
return dict(iter_stat())
135135

136136

137-
def iter_dev():
138-
"""
139-
Iterate over network devices. Each item represents the information about one of
140-
the network devices in the system.
141-
"""
142-
with DEV_PATH.open() as fobj:
143-
lines = fobj.readlines()
144-
# Skip the header lines (usually first 2 lines)
145-
for line in lines[2:]:
146-
fields = line.strip().split()
147-
if not fields:
148-
continue
149-
yield {
150-
"interface": fields[0].rstrip(":"),
151-
"receive": {
152-
"bytes": int(fields[1]),
153-
"packets": int(fields[2]),
154-
"errs": int(fields[3]),
155-
"drop": int(fields[4]),
156-
"fifo": int(fields[5]),
157-
"frame": int(fields[6]),
158-
"compressed": int(fields[7]),
159-
"multicast": int(fields[8]),
160-
},
161-
"transmit": {
162-
"bytes": int(fields[9]),
163-
"packets": int(fields[10]),
164-
"errs": int(fields[11]),
165-
"drop": int(fields[12]),
166-
"fifo": int(fields[13]),
167-
"colls": int(fields[14]),
168-
"carrier": int(fields[15]),
169-
"compressed": int(fields[16]),
170-
},
171-
}
172-
173-
174-
def dev():
175-
"""
176-
Network devices info as a sequence of dictionaries, each with information about one of
177-
the system network devices.
178-
"""
179-
return tuple(iter_dev())
180-
181-
182-
def iter_wireless():
183-
"""
184-
Iterate over wireless network devices. Each item represents the information about one of
185-
the wireless network devices in the system.
186-
"""
187-
with WIRELESS_PATH.open() as fobj:
188-
lines = fobj.readlines()
189-
# Skip the header lines (usually first 2 lines)
190-
for line in lines[2:]:
191-
fields = line.strip().split()
192-
if not fields:
193-
continue
194-
yield {
195-
"interface": fields[0].rstrip(":"),
196-
"status": int(fields[1], 16),
197-
"quality": {
198-
"link": int(fields[2].rstrip(".")),
199-
"level": int(fields[3].rstrip(".")),
200-
"noise": int(fields[4].rstrip(".")),
201-
},
202-
"discarded": {
203-
"nwid": int(fields[5]),
204-
"crypt": int(fields[6]),
205-
"misc": int(fields[7]),
206-
},
207-
}
208-
209-
210-
def wireless():
211-
"""
212-
Wireless netowrk devices info as a sequence of dictionaries, each with information about one of
213-
the system wireless network devices.
214-
"""
215-
return tuple(iter_wireless())
216-
217-
218-
def iter_netstat():
219-
"""
220-
Iterate over network statistics.
221-
"""
222-
return _iter_read_kv(NETSTAT_PATH)
223-
224-
225-
def netstat():
226-
"""
227-
Network statistics.
228-
"""
229-
return dict(iter_netstat())
230-
231-
232-
def iter_snmp():
233-
"""
234-
Iterate over SNMP statistics.
235-
"""
236-
return _iter_read_kv(SNMP_PATH)
237-
238-
239-
def snmp():
240-
"""
241-
SNMP statistics.
242-
"""
243-
return dict(iter_snmp())
137+
class net:
138+
@staticmethod
139+
def iter_devices():
140+
"""
141+
Iterate over network devices. Each item represents the information about one of
142+
the network devices in the system.
143+
"""
144+
with NET_DEV_PATH.open() as fobj:
145+
lines = fobj.readlines()
146+
# Skip the header lines (usually first 2 lines)
147+
for line in lines[2:]:
148+
fields = line.strip().split()
149+
if not fields:
150+
continue
151+
yield {
152+
"interface": fields[0].rstrip(":"),
153+
"receive": {
154+
"bytes": int(fields[1]),
155+
"packets": int(fields[2]),
156+
"errs": int(fields[3]),
157+
"drop": int(fields[4]),
158+
"fifo": int(fields[5]),
159+
"frame": int(fields[6]),
160+
"compressed": int(fields[7]),
161+
"multicast": int(fields[8]),
162+
},
163+
"transmit": {
164+
"bytes": int(fields[9]),
165+
"packets": int(fields[10]),
166+
"errs": int(fields[11]),
167+
"drop": int(fields[12]),
168+
"fifo": int(fields[13]),
169+
"colls": int(fields[14]),
170+
"carrier": int(fields[15]),
171+
"compressed": int(fields[16]),
172+
},
173+
}
174+
175+
@staticmethod
176+
def devices():
177+
"""
178+
Network devices info as a sequence of dictionaries, each with information about one of
179+
the system network devices.
180+
"""
181+
return tuple(net.iter_devices())
182+
183+
@staticmethod
184+
def iter_wireless():
185+
"""
186+
Iterate over wireless network devices. Each item represents the information about one of
187+
the wireless network devices in the system.
188+
"""
189+
with WIRELESS_PATH.open() as fobj:
190+
lines = fobj.readlines()
191+
# Skip the header lines (usually first 2 lines)
192+
for line in lines[2:]:
193+
fields = line.strip().split()
194+
if not fields:
195+
continue
196+
yield {
197+
"interface": fields[0].rstrip(":"),
198+
"status": int(fields[1], 16),
199+
"quality": {
200+
"link": int(fields[2].rstrip(".")),
201+
"level": int(fields[3].rstrip(".")),
202+
"noise": int(fields[4].rstrip(".")),
203+
},
204+
"discarded": {
205+
"nwid": int(fields[5]),
206+
"crypt": int(fields[6]),
207+
"misc": int(fields[7]),
208+
},
209+
}
210+
211+
@staticmethod
212+
def wireless():
213+
"""
214+
Wireless netowrk devices info as a sequence of dictionaries, each with information about one of
215+
the system wireless network devices.
216+
"""
217+
return tuple(net.iter_wireless())
218+
219+
@staticmethod
220+
def iter_netstat():
221+
"""
222+
Iterate over network statistics.
223+
"""
224+
return _iter_read_kv(NETSTAT_PATH)
225+
226+
@staticmethod
227+
def netstat():
228+
"""
229+
Network statistics.
230+
"""
231+
return dict(net.iter_netstat())
232+
233+
@staticmethod
234+
def iter_snmp():
235+
"""
236+
Iterate over SNMP statistics.
237+
"""
238+
return _iter_read_kv(SNMP_PATH)
239+
240+
@staticmethod
241+
def snmp():
242+
"""
243+
SNMP statistics.
244+
"""
245+
return dict(net.iter_snmp())
244246

245247

246248
def iter_pids():

0 commit comments

Comments
 (0)