/macstat

Bash shell script that collects macOS system metrics for Devo Data Analytics Platform

Primary LanguageShellMIT LicenseMIT

macstat

Bash shell script that collects macOS system metrics for Devo Data Analytics Platform.

It collects almost 30 system metrics in a log file ready to be sent to Devo using a third-party syslog application such as syslog-ng. Dstat and Devo Monitor inspired this project.

Prerequisites

1. Disable DTrace restrictions.

macstat uses iotop to get disk stats. This command is, in fact, a DTrace script. DTrace (Dynamic Tracing) is a powerful debugging framework. iotop is just one of the numerous DTrace scripts shipped by default on macOS. To run it, you need root privileges and tell Apple's System Integrity Protection (SIP) to let DTrace do its job. To do so, boot your Mac into recovery mode and run:

csrutil enable --without dtrace

Then restart your machine. Oh and ignore the scary output message. Trust me, it’s better to have an unsupported configuration than disabling SIP altogether.

Note: If you don't want or can't disable SIP, then use iostat instead. You'll need additional calculations, but to get you started I included a sample command in the comments to get data transferred in bytes/sec.

2. Compile networkStats.c file.

Execute the following command to compile this C file:

cc networkStats.c -DWEBVIEW_COCOA=1 -ObjC -framework Cocoa -framework WebKit -o networkStats

This creates a binary file called networkStats that macstat will use to get network data sent and received in bytes/sec.

Note: I'm not the author of this C file. I just found it on the Internet, but I don't remember the source.

3. Move iotop script to /usr/local/bin.

This is a custom iotop script. I added improved settings recommended by iotop's author in his book to address dynamic variable drops errors. As mentioned in step 1, iotop requires DTrace enabled and root privileges. Using the default /usr/bin/iotop command will show inaccurate results.

Note: If you are using iostat, skip this step.

4. Edit the USER SETTINGS section.

Replace the variables in the USER SETTINGS section with your own values accordingly.

Testing

USAGE: sudo ./macstat [options]

  -a      list all system metrics
  -h      show help 
  -V      display version info

For testing, run macstat on itself:

sudo ./macstat

If it was successful, you should get this message:

macstat finished successfully
Log: /Users/roberto/Library/Logs/Devo/macstat.log

Run it!

Add it to the root crontab to be scheduled to run every minute:

* * * * * /Users/roberto/macstat

Note: If you are not using iotop, then add it to your own user crontab.

Sending macstat log file to Devo

In the extras directory, I provided the config files syslog-ng.conf and syslog-ng.conf.relay to send macstat.log to Devo using syslog-ng depending on your environment. Also, I included the config file devo.conf, which uses the default log file rotation utility in macOS: newsyslog. For more information on how to implement these files, review my Medium guide.

License

macstat is licensed under the terms of the MIT License. See the LICENSE file for details.

Contact

Find me as rcmelendez on LinkedIn, Medium, and of course GitHub.