summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPeter Kasza <mr.schyte@gmail.com>2012-04-14 22:05:37 +0200
committerPeter Kasza <mr.schyte@gmail.com>2012-04-14 22:05:37 +0200
commitdaa114a3979105fbbbf53930267c8a667ec9ae59 (patch)
treed1b0ec93c5139c6f7ed655c3123c185b908910c6
parentdaf28d5b756618290e9cf3938c4cb3f2f8f98a22 (diff)
downloadxmobar-daa114a3979105fbbbf53930267c8a667ec9ae59.tar.gz
xmobar-daa114a3979105fbbbf53930267c8a667ec9ae59.tar.bz2
net: hide output when interface is down.
-rw-r--r--src/Plugins/Monitors/Net.hs32
1 files changed, 26 insertions, 6 deletions
diff --git a/src/Plugins/Monitors/Net.hs b/src/Plugins/Monitors/Net.hs
index 1513e07..4951169 100644
--- a/src/Plugins/Monitors/Net.hs
+++ b/src/Plugins/Monitors/Net.hs
@@ -22,10 +22,14 @@ import Data.Time.Clock (UTCTime, getCurrentTime, diffUTCTime)
import qualified Data.ByteString.Lazy.Char8 as B
+data OperState = Up | Down
+ deriving (Eq, Show, Read)
+
data NetDev = NA
| ND { netDev :: String
, netRx :: Float
, netTx :: Float
+ , netOperState :: OperState
} deriving (Eq,Show,Read)
type NetDevRef = IORef (NetDev, UTCTime)
@@ -53,12 +57,26 @@ wordsBy f s = case dropWhile f s of
readNetDev :: [String] -> NetDev
readNetDev [] = NA
readNetDev xs =
- ND (head xs) (r (xs !! 1)) (r (xs !! 2))
+ ND (head xs) (r (xs !! 1)) (r (xs !! 2)) Down
where r s | s == "" = 0
| otherwise = read s / 1024
+
+parseOperState :: String -> OperState
+parseOperState state | state == "down" = Down
+ | otherwise = Up
+
+fillOperState :: NetDev -> IO NetDev
+fillOperState (ND d r t _) = do
+ operstate <- B.readFile $ "/sys/class/net/" ++ d ++ "/operstate"
+ return (ND d r t (parseOperState $ (B.unpack . head . B.lines) operstate))
+
+fillOperState NA = return NA
+
fileNet :: IO [NetDev]
-fileNet = netParser `fmap` B.readFile "/proc/net/dev"
+fileNet = do
+ devs <- netParser `fmap` B.readFile "/proc/net/dev"
+ mapM fillOperState devs
findNetDev :: String -> IO NetDev
findNetDev dev = do
@@ -83,9 +101,11 @@ formatNet d = do
printNet :: NetDev -> Monitor String
printNet nd =
case nd of
- ND d r t -> do (rx, rb) <- formatNet r
- (tx, tb) <- formatNet t
- parseTemplate [d,rx,tx,rb,tb]
+ ND d r t o -> case o of
+ Up -> do (rx, rb) <- formatNet r
+ (tx, tb) <- formatNet t
+ parseTemplate [d,rx,tx,rb,tb]
+ Down -> return ""
NA -> return "N/A"
parseNet :: NetDevRef -> String -> IO NetDev
@@ -99,7 +119,7 @@ parseNet nref nd = do
netRate f da db = takeDigits 2 $ (f db - f da) / scx'
diffRate NA _ = NA
diffRate _ NA = NA
- diffRate da db = ND nd (netRate netRx da db) (netRate netTx da db)
+ diffRate da db = ND nd (netRate netRx da db) (netRate netTx da db) (netOperState db)
return $ diffRate n0 n1
runNet :: NetDevRef -> String -> [String] -> Monitor String