2

I'm developing a script that will monitor several machines, notably by reading from /proc/[vmstat|stat|diskstats|net/dev] and I'm wondering if I can assume those will always be accessible and readable or if I should check whether the read went well. Any advice?

terdon
  • 242,166
Nico
  • 225

1 Answers1

4

Strictly speaking, you cannot count on /proc to be there. Very security minded admins my chose not to even mount it.

But if it is there, it's quite safe to assume everybody can read it. Quite a few very common command line tools depend on /proc, (e.g. ps, top).

Hkoof
  • 1,667
  • Okay, thanks. And if it's there, can I also assume that it will not be temporarily unavailable - even for a very improbable reason? – Nico Oct 08 '18 at 14:12
  • Are you asking me if it is probable for something very improbable to happen? – Hkoof Oct 08 '18 at 14:24
  • No, I'm asking if it's at all possible :-) – Nico Oct 08 '18 at 14:31
  • 1
    As a linux sysadmin for over 10 years, I never saw it happen in 200+ machines. I'd say only a serious disk or admin failure, or an unlikely, big kernel bug could cause /proc to be suddenly temporarily unavailable just like that. – Hkoof Oct 08 '18 at 14:51
  • I saw it happen once, after not adding it to /etc/mount after installing linux compatability on a BSD (unix) server. Only time I saw it not being there though. – Hennes Oct 08 '18 at 15:41
  • 1
    Not quite true, you may not be able to read the contents of /proc/$PID directories you don't own. It's not common, but some people do mount /proc with hidepid=1. – Austin Hemmelgarn Oct 08 '18 at 19:37
  • @Austin Hemmelman: that, and "[...] it will not be temporarily unavailable" are not exactly the same thing. – Hkoof Oct 09 '18 at 06:25
  • I should have clarified, I was commenting on the 'If it's there, it's quite safe to assume everybody can read it.' bit in the answer itself. It's still technically safe, but you need to handle -EPERM errors correctly. – Austin Hemmelgarn Oct 09 '18 at 11:45