diff options
author | Reto Habluetzel <rethab@rethab.ch> | 2012-09-04 07:10:26 +0200 |
---|---|---|
committer | Reto Habluetzel <rethab@rethab.ch> | 2012-09-04 07:10:26 +0200 |
commit | 431b8eaffd865593bed4e1db1843452a1f1fe64c (patch) | |
tree | ff6cfa4826d9522f2ebba61519d0efbafc0e2ebe /src/Plugins/Monitors/Net.hs | |
parent | 2cf7e19b8be5e892c03bd95cf4e1acffe0e53e66 (diff) | |
download | xmobar-431b8eaffd865593bed4e1db1843452a1f1fe64c.tar.gz xmobar-431b8eaffd865593bed4e1db1843452a1f1fe64c.tar.bz2 |
automatic detection of devices using /sys/class/net directory
Diffstat (limited to 'src/Plugins/Monitors/Net.hs')
-rw-r--r-- | src/Plugins/Monitors/Net.hs | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/Plugins/Monitors/Net.hs b/src/Plugins/Monitors/Net.hs index 10ca9cc..8234670 100644 --- a/src/Plugins/Monitors/Net.hs +++ b/src/Plugins/Monitors/Net.hs @@ -22,7 +22,9 @@ import Plugins.Monitors.Common import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Data.Time.Clock (UTCTime, getCurrentTime, diffUTCTime) -import Control.Monad (forM) +import Control.Monad (forM, filterM) +import System.Directory (getDirectoryContents, doesFileExist) +import System.FilePath ((</>)) import qualified Data.ByteString.Lazy.Char8 as B @@ -32,10 +34,9 @@ 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. - -} +-- 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 @@ -54,9 +55,18 @@ 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 @@ -139,13 +149,15 @@ startNet i a r cb = do _ <- parseNet nref i runM a netConfig (runNet nref i) r cb -startDynNet :: [String] -> [String] -> Int -> (String -> IO ()) -> IO () -startDynNet is a r cb = do - refs <- forM is $ \i -> do +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 i - return (nref, i) + _ <- parseNet nref d + return (nref, d) runM a netConfig (runNets refs) r cb -- TODO: Prelude.head: empty list +-- TODO: remember last active interface. |