summaryrefslogtreecommitdiffhomepage
path: root/src/Plugins/Monitors/Net.hs
diff options
context:
space:
mode:
authorJose Antonio Ortega Ruiz <jao@gnu.org>2012-09-15 21:04:55 +0200
committerJose Antonio Ortega Ruiz <jao@gnu.org>2012-09-15 21:04:55 +0200
commitacdbb2a628f9b3876a5f97f0c2e6ae6a85d6c290 (patch)
treef756277a9f061c86cbb4de823ac1f034f6a2fd8b /src/Plugins/Monitors/Net.hs
parent3e7c8cf1a4cd9ea86fd7d1dce13e305dd09fb6fe (diff)
parente2407925093dfcc4e89dabeceb46429adda015b9 (diff)
downloadxmobar-acdbb2a628f9b3876a5f97f0c2e6ae6a85d6c290.tar.gz
xmobar-acdbb2a628f9b3876a5f97f0c2e6ae6a85d6c290.tar.bz2
Merge remote-tracking branch 'rethab/master' into rethab
Diffstat (limited to 'src/Plugins/Monitors/Net.hs')
-rw-r--r--src/Plugins/Monitors/Net.hs53
1 files changed, 51 insertions, 2 deletions
diff --git a/src/Plugins/Monitors/Net.hs b/src/Plugins/Monitors/Net.hs
index 768907c..b8adc74 100644
--- a/src/Plugins/Monitors/Net.hs
+++ b/src/Plugins/Monitors/Net.hs
@@ -13,12 +13,18 @@
--
-----------------------------------------------------------------------------
-module Plugins.Monitors.Net (startNet) where
+module Plugins.Monitors.Net (
+ startNet
+ , startDynNet
+ ) where
import Plugins.Monitors.Common
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import Data.Time.Clock (UTCTime, getCurrentTime, diffUTCTime)
+import Control.Monad (forM, filterM)
+import System.Directory (getDirectoryContents, doesFileExist)
+import System.FilePath ((</>))
import qualified Data.ByteString.Lazy.Char8 as B
@@ -28,14 +34,39 @@ data NetDev = NA
type NetDevRef = IORef (NetDev, UTCTime)
+-- The more information available, the better.
+-- Note that names don't matter. Therefore, if only the names differ,
+-- a compare evaluates to EQ while (==) evaluates to False.
+instance Ord NetDev where
+ compare NA NA = EQ
+ compare NA _ = LT
+ compare _ NA = GT
+ compare (NI _) (NI _) = EQ
+ compare (NI _) (ND _ _ _) = LT
+ compare (ND _ _ _) (NI _) = GT
+ compare (ND _ x1 y1) (ND _ x2 y2) =
+ if downcmp /= EQ
+ then downcmp
+ else y1 `compare` y2
+ where downcmp = x1 `compare` x2
+
netConfig :: IO MConfig
netConfig = mkMConfig
"<dev>: <rx>KB|<tx>KB" -- template
["dev", "rx", "tx", "rxbar", "txbar"] -- available replacements
+operstateDir :: String -> FilePath
+operstateDir d = "/sys/class/net" </> d </> "operstate"
+
+existingDevs :: IO [String]
+existingDevs = getDirectoryContents "/sys/class/net" >>= filterM isDev
+ where isDev d | d `elem` excludes = return False
+ | otherwise = doesFileExist (operstateDir d)
+ excludes = [".", "..", "lo"]
+
isUp :: String -> IO Bool
isUp d = do
- operstate <- B.readFile $ "/sys/class/net/" ++ d ++ "/operstate"
+ operstate <- B.readFile (operstateDir d)
return $ "up" == (B.unpack . head . B.lines) operstate
readNetDev :: [String] -> IO NetDev
@@ -103,9 +134,27 @@ parseNet nref nd = do
runNet :: NetDevRef -> String -> [String] -> Monitor String
runNet nref i _ = io (parseNet nref i) >>= printNet
+parseNets :: [(NetDevRef, String)] -> IO [NetDev]
+parseNets = mapM $ \(ref, i) -> parseNet ref i
+
+runNets :: [(NetDevRef, String)] -> [String] -> Monitor String
+runNets refs _ = io (parseActive refs) >>= printNet
+ where parseActive refs' = parseNets refs' >>= return . selectActive
+ selectActive = maximum
+
startNet :: String -> [String] -> Int -> (String -> IO ()) -> IO ()
startNet i a r cb = do
t0 <- getCurrentTime
nref <- newIORef (NA, t0)
_ <- parseNet nref i
runM a netConfig (runNet nref i) r cb
+
+startDynNet :: [String] -> Int -> (String -> IO ()) -> IO ()
+startDynNet a r cb = do
+ devs <- existingDevs
+ refs <- forM devs $ \d -> do
+ t <- getCurrentTime
+ nref <- newIORef (NA, t)
+ _ <- parseNet nref d
+ return (nref, d)
+ runM a netConfig (runNets refs) r cb