Project 37. Profile the Hardware"How do I view the hardware and power management settings of my Mac?" This project shows you how to run Apple's System Profiler from the command line and how to extract specific details from its output. Then it looks at utilities for viewing and configuring the power management hardware (Energy Saver settings). It covers the commands system_profiler, pmset, arch, machine, and hostinfo. Learn More
Learn More
Profile from the Command LineYou may be familiar with Apple's System Profiler. If not, choose Apple > About This Mac and click the More Info button to launch System Profiler. You'll see a window where you may browse for information about hardware, software, and network settings. The same information is delivered by system_profiler, the command-line version of the tool. It returns a lot of information, so pipe the output to the less command, or redirect it to a file, for easy viewing. $ system_profiler | less The tool presents information as plain text. As such, it makes a suitable grepping victim when you need to filter for specific details. The next section shows how we might use this information to monitor disk drives for impending failure. Be SmartA heavily used drive like your system disk may start developing faults after just 3 years of use. It's prudent to replace the drive before it fails, but how do you know when a drive is about to fail? Get SMART. Most modern hard-disk drives use a technology called SMART (Self-Monitoring, Analysis, and Reporting Technology) to detect and predict impending problems. We'll use system_profiler to check drives' SMART status. Almost all the configuration data you could want is lurking somewhere in the output from system_profiler. To use it, we extract just those fragments that interest us. Before attempting this, examine the output from system_profiler to determine how the relevant data can be consistently and uniquely identified; then choose the appropriate text-filtering utilities to grab it. Note
Perusal of system_profiler output reveals that the best search term for our target data is S.M.A.R.T. (humans dropped dots from the acronym, system_profiler hasn't). Output from a Mac with multiple drives also tells us we should capture the two lines that follow each match, to be sure which drive's status is reported in the matched line. Learn More
To check the SMART status of all mounted drives, then, we'll filter system_profiler output with fgrep, using option -A2 to capture two lines of context in addition to each matching line. $ system_profiler | fgrep -A2 "S.M.A.R.T." S.M.A.R.T. status: Not Supported Volumes: Backup: -- S.M.A.R.T. status: Verified Volumes: Macintosh HD: The output reports on two drives: one that does not support SMART, and the main system drive, which does. The system drive's status is Verified, which is good, and the drive is healthy; if it reported About to Fail, it'd be time to back up the disk and replace it. Ironically, we chose to search for S.M.A.R.T. with fgrep because that command is less smart than grep : Because fgrep does not support regular expressions, it doesn't require us to escape the five periods grep would have treated as special characters. Adding this SMART-status command to a bash startup file (such as /etc/profile) lets you keep regular tabs on your disk drives. If you don't want drive status displayed every time you start bash, change the search term to "About to Fail", and you'll see nothing unless SMART issues that report: No news is good news. Profiler TipsSuppose we're writing a Bash script that acts on the IP address of every router mentioned in the network configuration (as shown in the Network section of System Preferences). Here's how we might do so, using the now-familiar grep filtering technique on the output from system_profiler. Learn More
Learn More
$ system_profiler | grep "Router:" Router: 217.155.168.150 Router: 10.0.2.1 Router: 217.155.168.150 Router: 10.0.2.1 Next, we refine the search by employing the awk utility to print just the second field of each linethe field that contains the IP address. $ system_profiler | grep "Router:" | awk '{print $2}' 217.155.168.150 10.0.2.1 217.155.168.150 10.0.2.1 Finally, to skip duplicate IP addresses, we sort the lines into order with sort and drop consecutive duplicate lines with uniq. $ system_profiler | grep "Router:" | awk '{print $2}' ¬ | sort | uniq 10.0.2.1 217.155.168.150 Profiler OptionsThe system_profiler command uses options that specify which hardware to profile and how much information to return. The default setting for the detail-level option, -detaillevel, is basic. It reports personal information. To omit that information, request a detail level of mini instead. $ system_profiler -detaillevel mini A detail level of full reports lots more information than basic, including a list of all installed applications. Note
You can limit a profiler report to specific hardware subsystems or software categories by specifying datatypes as arguments to system_profiler. Discover the possibilities by typing $ system_profiler -listdatatypes Available Datatypes: ... Tip
We could rewrite our SMART example from earlier in this project to be a little more selective (and to run quicker) by confining it to internal serial ATA drives. $ system_profiler -detaillevel mini SPSerialATADataType ¬ | fgrep "S.M.A.R.T." S.M.A.R.T. status: Verified Finally, specify the option -xml, and system_profiler will produce reports written in XML format. XML (eXtensible Markup Language) is used extensively in Mac OS X for configuration and preference files. Write the report to a file with the extension .spx, and you'll get a double-clickable document that opens in the graphical System Profiler. $ system_profiler -xml > profile.spx Display Processor and RAM InformationA couple of standard Unix commands are available to report on the architecture (processor family) and machine (processor type). $ arch ppc $ machine ppc970 Control Power ManagementApple provides a command-line tool, pmset, for viewing and setting Energy Saver preferences. It's useful when you need to change such settings from the command line or a script. You'll see the same settings in the System Preferences Energy Saver pane. Tip
View the current setting by typing $ pmset -g Active Profiles: AC Power -1* Currently in use: womp 1 ... displaysleep 22 dps 1 Tip
Power management settings (or profiles) are maintained individually for AC (mains), battery, and UPS (Uninterruptible Power Supply) power sources. The example above shows the current (live) profile for AC power. To display profiles for all power sources, type $ pmset -g disk (Settings are saved on disk.) You'll see only those profiles that are relevant to your hardware. Desktop Macs won't list battery settings, for example. To change settings, you must run pmset as the root user, just as you must authenticate when changing Energy Saver settings in System Preferences. To set the Display Sleep time to 22 minutes, type the following, and give your administrator password at the prompt. $ sudo pmset displaysleep 22 Password: Learn More
|