pythonlinuxinotifyprocfsfile-monitoring

monitoring proc mounts using poll or select


I am trying to come up with a script that will monitor /proc/mounts and notify back when it detects a read only file system.

In python, one way is to store the value of /proc/mounts in a list and keep doing a cat /proc/mounts in a loop and check for "ro" entity directly. But i want to use poll or select instead of this since that's efficient and act only when an event has come.

I see that poll is preferable to select. From what i understand, we could put the fd of /proc/mounts in exceptfds to select and we will be notified when there are exceptions(please correct me here, changes in line are signalled as exceptions?). Just for testing, i am trying it on a regular file - now, is it expected that when i open and make changes to e.txt this file will print the whole file ? Currently, it does not. What am i missing ?

import select

f = open("e.txt")
while True:
    r,w,x = select.select([],[],[f])
    f.seek(0)
    print f.read()
    < check for ro entity and do further>

For /proc/mounts monitoring:

import select

f = open("/proc/mounts")
while True:
    r,w,x = select.select([],[],[f])
    f.seek(0)
    print f.read()
    < check for ro entity and do further>

How can I achieve this using select or poll.


Solution

  • This does work for /proc/mounts monitoring. Any changes to lines are treated as exception and hence adding the fd of /proc/mounts to exceptfds(which is the third parameter in select of select.select([],[],[f]))

    import select
    
    f = open("e.txt")
    while True:
        r,w,x = select.select([],[],[f])
        f.seek(0)
        print f.read()