|
之前我们你已经说过了led管控机制* z6 ?7 N: q8 p @- m8 O/ g" O
聊一聊x3p的LED控制机制: z+ `% v1 Z" F
和用led来显示硬盘温度
2 {7 S6 e! s, D4 ^1 p3 C让你的RGB灯来指示系统温度) p; m8 \" ^/ e% j, t; _' y
现在我们可以进一步让led来显示系统网速,且往下看
) t' [0 }6 \* V4 H; C需要用到一个python脚本来获取系统的联网状态以及各个io状态& k; m9 P ]8 y8 S" ^! t7 t4 ~
- import psutil9 Q3 T( l3 x3 G( Y# A5 S1 x. B
- print(psutil.net_io_counters(pernic=True))
复制代码 输出结果是下面的3 B; x8 X, }9 P8 G9 e5 Y
- {9 m" b: L; @) m6 q& h% u
- 'bond0': snetio(bytes_sent = 0, bytes_recv = 0, packets_sent = 0, packets_recv = 0, errin = 0, errout = 0, dropin = 0, dropout = 0),' _: b4 d( j. L( @1 h0 V2 f
- 'lo': snetio(bytes_sent = 371531983, bytes_recv = 371531983, packets_sent = 649842, packets_recv = 649842, errin = 0, errout = 0, dropin = 0, dropout = 0),
6 U9 I: L- ~3 y u* ~* a - 'tun0': snetio(bytes_sent = 300576, bytes_recv = 0, packets_sent = 9393, packets_recv = 0, errin = 0, errout = 0, dropin = 0, dropout = 0),3 x. F6 |, A; k
- 'sit0': snetio(bytes_sent = 0, bytes_recv = 0, packets_sent = 0, packets_recv = 0, errin = 0, errout = 0, dropin = 0, dropout = 0),. S- G0 L9 ?' Y( Q! e& c! O
- 'ip_vti0': snetio(bytes_sent = 0, bytes_recv = 0, packets_sent = 0, packets_recv = 0, errin = 0, errout = 0, dropin = 0, dropout = 0),
3 k( }4 {& `3 M* R' c6 w$ W - 'eth0': snetio(bytes_sent = 10911429402, bytes_recv = 387312110, packets_sent = 722569, packets_recv = 1194110, errin = 0, errout = 0, dropin = 5, dropout = 0)
G0 P9 L: Y5 z - }
复制代码 有了数据就能够处理数据转化为网速了。
: Z2 f& x& F5 j# z
$ k$ Q3 r' R& t3 k$ {( w3 |
因为网速差别实在太大,所以我用了分级显示的方案:
6 {; f, c$ |$ H; }) o7 o网速为B级别时显示红灯,网速越大,红灯越亮,不过放心,几乎看不到红灯( T, M( |8 {% {, x, P+ o
网速为K级别时显示绿灯,网速越快,绿灯越绿。这个比较常见,几乎上硬盘待机时就一直是绿灯+ p" u/ r, H) n* ]2 o; E% ]: |
网速为M级别时显示蓝灯,网速越高,蓝灯越艳丽,当你下载东西或者往硬盘传文件时就是蓝灯了。3 W/ m9 [9 b8 _, l# o: k
- net_led_dn() {' e( q C. O1 O# K4 J* m
- python /root/netspeed.py | sed -e "s/'/\n/g" -e 's/, /=/g' | grep } | awk -F '=' '{print $4}' >/root/new.rtx
' z5 R$ O6 U6 f9 X" L - new_rtx=$(cat /root/new.rtx)4 a0 K, Q( L* F* n
- old_rtx=$(cat /root/rtx.log) x* k0 h! Z+ C6 r3 {, E) H
- rtx=$(expr $new_rtx - $old_rtx)2 n- O$ I, s" a' U& i$ T
- echo $rtx/ X8 e6 K5 ^) G( B9 `& ~
- if [ $rtx -lt 1024 ]; then
& Z% e, U F& S. D* Q) Y1 U1 z -
5 [/ |; L9 L8 w% q% |2 x. J3 f - rtx_led=$(expr $rtx / 4)
# p8 Y- k3 l/ o: L4 E; t. K8 J -
t2 ]' ]& k5 b' j8 ?3 C4 q0 i3 ? - set_led c1 $rtx_led 0% S* x" n' n$ R1 Y+ M4 u% r4 o5 k
- set_led c2 0 0
) u9 ?- L1 U' x, r: h - set_led c3 0 08 B% }5 h% O! i! I
-
9 F) R6 O) F6 b& b0 W' @ X - elif [ $rtx -lt 1048576 ]; then
' _+ F# @4 c/ N - rtx_led=$(expr $rtx / 4096)& {+ _$ ^. |3 ^, b+ ?
- set_led c1 0 0
+ m1 B1 B9 | h( C - set_led c2 $rtx_led 0. Z* P# Q* S8 D
- set_led c3 0 03 ^$ d! w' w# ~; B
- elif [ $rtx -lt 1073741824 ]; then
- G/ E9 p( ^, `$ r* N, Z6 } - rtx_led=$(expr $rtx / 4194304)
" @3 v. \& `* N& Y - set_led c1 0 0
. a! b+ v" W. y - set_led c2 0 0
3 q7 W! z5 C! Y& [# Y - set_led c3 $rtx_led 0
9 r2 d6 i, M7 Z) y9 K% v: p" U4 S - fi- N4 \# H# l o: ?+ e( T; H: s) ~
- echo $new_rtx >/root/rtx.log, x2 I: j3 g6 U: E; {
- }
复制代码 这个只是显示下载网速的,如果想要显示上传网速也很简单,把第二行的$4改为$2就行了。2 U! d" U% {0 U
下面是完整脚本,看附件吧。( j4 a$ a: t- s/ Q8 P, c
一共有五个文件
, _) ^2 s# V! W2 B; n+ L! E& p- ledstatus.sh #放在/opt/bin文件夹下替换) ]* U, E e" h
- netspeed.py #放在root下
8 z+ \" h5 M, |( ` - new.rt #放在root下* S/ q; ~* o8 Y1 R) K+ W% y
- mew.rtx #放在root下, O- K+ z- E; K7 g; ~: B
- rtx #放在root下
复制代码
; a0 S1 e) W; s- O' T, b( v6 v, t8 s- q0 ~
led_control.zip
(1015 Bytes, 下载次数: 114)
' X* E) y2 E8 F+ c, }- R% V. n
解压密码(刮刮乐):chwhsen
7 t0 q! ~0 J" ~
* O* R( a* {8 b* [5 t U
) n/ A7 t: R. o) Q5 \7 U! g* _
* O+ o) h4 j& P( `3 ` |
评分
-
查看全部评分
|