LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Easily monitor power management features (https://www.linuxquestions.org/questions/slackware-14/easily-monitor-power-management-features-4175637620/)

denydias 09-02-2018 03:31 AM

Easily monitor power management features
 
1 Attachment(s)
This thread sparked a feeling on me: I know how to look for power management status, but I need to type a lot to know everything I could also learn with a single short command.

pm-mon is what came out of this spark.

A short demo is attached. Installation, usage and other info is available here.

One question to the fellow experienced users: I did what I could to obtain HDD spin status without the need to use hdparm and smartctl (both requiring root), but I was beaten on this quest. Is there any way to extract the HDD spin status from /proc, /sys or any other tool not requiring privilege escalation to provide useful readouts? Thanks in advance for any tips.

Enjoy!

abga 09-02-2018 09:25 PM

No idea how to obtain such info without being root.
Is it an issue if you make use of sudo and restrict what the user running pm-mon is able to execute as root?
You could achieve that by running visudo and adding the following line (substitute user with your actual user):
Code:

user ALL=(ALL) NOPASSWD:/usr/sbin/hdparm
#or
user ALL=(ALL) NOPASSWD:/usr/sbin/smartctl


denydias 09-02-2018 10:34 PM

Quote:

Originally Posted by abga (Post 5899205)
No idea how to obtain such info without being root.
Is it an issue if you make use of sudo and restrict what the user running pm-mon is able to execute as root?
You could achieve that by running visudo and adding the following line (substitute user with your actual user):
Code:

user ALL=(ALL) NOPASSWD:/usr/sbin/hdparm
#or
user ALL=(ALL) NOPASSWD:/usr/sbin/smartctl


Thank you for the suggestion, @abga.

No, it's not. But this is not up to me. This should be a user choice. It will work the same if the user do this:

Code:

chmod u+s /usr/sbin/smartctl
The above will not require add anything to /etc/sudoers. It'll just run smartctl as root, regardless of who called it. I don't want to impose that security hole to users as smartctl allows a number of operations on that we don't need for the scope of pm-mon.

hdparm is not an option here as its -C parameter (to query HDD power state) reportedly disturbs the HDD and put it to spin.

But thanks anyway for the ideia.

abga 09-03-2018 04:46 AM

Quote:

Originally Posted by denydias (Post 5899215)
Thank you for the suggestion, @abga.

No, it's not. But this is not up to me. This should be a user choice. It will work the same if the user do this:

Code:

chmod u+s /usr/sbin/smartctl
The above will not require add anything to /etc/sudoers. It'll just run smartctl as root, regardless of who called it.

Using sudo (/etc/sudoers) and restricting the usage of a specific binary for a specific user is a far safer security approach IMO.

Maybe consider giving the udisks or udisksctl utilities a try, you might be able to extract some info about the PM state of the drive:
https://linux.die.net/man/1/udisks
http://storaged.org/doc/udisks2-api/...isksctl.1.html

denydias 09-03-2018 04:53 AM

Quote:

Originally Posted by abga (Post 5899318)
Maybe consider giving the udisks or udisksctl utilities a try, you might be able to extract some info about the PM state of the drive:
https://linux.die.net/man/1/udisks
http://storaged.org/doc/udisks2-api/...isksctl.1.html

Yeah, while developing I tried udisks too. It says only if a HDD can spindown, but don't provide the spin status.

Didn't know about udisksctl. Thanks for that, but it also do not provide HDD spin hints.

abga 09-03-2018 05:05 AM

Sorry, no other ideas ATM.
Frankly, I always disable the PM of the hard drive with /usr/sbin/hdparm -B 255 /dev/sdX (in rc.local), I know it's not recomended, especially not parking the heads, but I started to hate the clicking sounds in the newer models.

denydias 09-03-2018 05:23 AM

That's ok. You have helped anyway. Thanks for the time.

As for HDD PM, yes, the rattling noise is truly annoying. Fortunately my notebook has room for one HDD and two M2 SSDs, sda being the HDD. I've managed to install Slackware into sdb SSD then I can just be aggressive with sda's PM as I have very little use to it, like editing videos when I travel with my motorcycle. The rest of the time it just sits there disabled, so no noise.

abga 09-03-2018 05:44 AM

These might be helpful in interrogating the kernel about the pm state, if not, at least they'll keep you busy ;)
https://www.kernel.org/doc/html/v4.1...m/devices.html
http://www.infradead.org/~mchehab/ke...driver-api.pdf
https://www.kernel.org/doc/Documenta...runtime_pm.txt

Mark Pettit 09-03-2018 05:45 AM

Um - I'm gonna throw something into the mix here - possibly unexpected. I haven't checked exactly for what you're looking for, but this tool my just have it. And check here : https://osquery.io/schema/3.3.0 for the tables and columns to query. And if it doesn't do, maybe you'll get lucky if you ask the developers to add it :-)

denydias 09-03-2018 05:49 AM

Quote:

Originally Posted by abga (Post 5899336)
These might be helpful in interrogating the kernel about the pm state, if not, at least they'll keep you busy ;)

Sure thing! Thanks for the links.

I'll also take a look at smartctl source to see if I can mimic its calls in bash, although I'm not holding my breath about it. :p

denydias 09-03-2018 05:52 AM

Quote:

Originally Posted by Mark Pettit (Post 5899337)
Um - I'm gonna throw something into the mix here - possibly unexpected. I haven't checked exactly for what you're looking for, but this tool my just have it. And check here : https://osquery.io/schema/3.3.0 for the tables and columns to query. And if it doesn't do, maybe you'll get lucky if you ask the developers to add it :-)

Thanks for that, Mark! I'm going to look for the call I need. ;)

denydias 09-03-2018 09:16 PM

I gave a good look over smartctl and hdparm sources. Nope, there's no way to get HDD spin status from an unprivileged bash script. The kernel system calls to the SATA subsystems are well protected from the user space. Any operation issued to those calls requires privilege escalation, which is a VERY good thing!

I could implement a small C thing just to query HDD spin status, but that would require privilege escalation too. So it looks wiser to just keep smartctl and add extra checks if a user can run it as root.

This is how I did it:

1. If user is root, just run smartctl; else
2. If user can `sudo smartctl` without a password, do that; else
3. If smartctl is setuid, just run it as the user; else
4. If none of the above is true, then just print <root required>.

I've updated the script and the comment bellow it to reflect that changes. There are some other improvements too.

Thank you all that provided me some extra info here. ;)


All times are GMT -5. The time now is 06:03 AM.