diff options
author | Michal Zielonka <michal.zielonka.8001@gmail.com> | 2021-10-07 23:25:09 +0200 |
---|---|---|
committer | Michal Zielonka <michal.zielonka.8001@gmail.com> | 2021-10-08 11:11:11 +0200 |
commit | b99a8a6833a1b38882b463fd72784cd6d6f91d9e (patch) | |
tree | 9537dcfe5eff2108937bf9a2160a5f15a7a266e5 /src/Xmobar/Plugins/Monitors/Net/Linux.hs | |
parent | a845465fec735d9818a61d078337653b5293da5c (diff) | |
download | xmobar-b99a8a6833a1b38882b463fd72784cd6d6f91d9e.tar.gz xmobar-b99a8a6833a1b38882b463fd72784cd6d6f91d9e.tar.bz2 |
try to reorganize modules per os
We should make better split os specify code for FreeBSD and Linux.
Idea comes from @liskin.
Diffstat (limited to 'src/Xmobar/Plugins/Monitors/Net/Linux.hs')
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Net/Linux.hs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/Xmobar/Plugins/Monitors/Net/Linux.hs b/src/Xmobar/Plugins/Monitors/Net/Linux.hs new file mode 100644 index 0000000..9306497 --- /dev/null +++ b/src/Xmobar/Plugins/Monitors/Net/Linux.hs @@ -0,0 +1,69 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.Monitors.Net.Linux +-- Copyright : (c) 2011, 2012, 2013, 2014, 2017, 2020 Jose Antonio Ortega Ruiz +-- (c) 2007-2010 Andrea Rossato +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Jose A. Ortega Ruiz <jao@gnu.org> +-- Stability : unstable +-- Portability : unportable +-- +-- A net device monitor for Xmobar +-- + +----------------------------------------------------------------------------- + +{-# LANGUAGE OverloadedStrings #-} + +module Xmobar.Plugins.Monitors.Net.Linux ( + existingDevs + , findNetDev + ) where + +import Xmobar.Plugins.Monitors.Net.Common (NetDevRawTotal, NetDev(..), NetDevInfo(..)) + +import Control.Monad (filterM) +import System.Directory (getDirectoryContents, doesFileExist) +import System.FilePath ((</>)) +import System.IO.Error (catchIOError) +import System.IO.Unsafe (unsafeInterleaveIO) + +import qualified Data.ByteString.Char8 as B + + +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 = flip catchIOError (const $ return False) $ do + operstate <- B.readFile (operstateDir d) + return $! (head . B.lines) operstate `elem` ["up", "unknown"] + +readNetDev :: [String] -> IO NetDevRawTotal +readNetDev ~[d, x, y] = do + up <- unsafeInterleaveIO $ isUp d + return $ N d (if up then ND (r x) (r y) else NI) + where r s | s == "" = 0 + | otherwise = read s + +netParser :: B.ByteString -> IO [NetDevRawTotal] +netParser = mapM (readNetDev . splitDevLine) . readDevLines + where readDevLines = drop 2 . B.lines + splitDevLine = map B.unpack . selectCols . filter (not . B.null) . B.splitWith (`elem` [' ',':']) + selectCols cols = map (cols!!) [0,1,9] + +findNetDev :: String -> IO NetDevRawTotal +findNetDev dev = do + nds <- B.readFile "/proc/net/dev" >>= netParser + case filter isDev nds of + x:_ -> return x + _ -> return NA + where isDev (N d _) = d == dev + isDev NA = False |