Monitoring your Mac with Prometheus
Stephen Townshend
Tech Manager and Reliability, Observability and Resilience Engineer and Advocate
A few weeks ago I was exploring SquaredUp Cloud which is an dashboarding and visibility platform that lets you connect to data from many different tools and sources. In order to have some familiar data to work with I set up the Prometheus node exporter and Prometheus on my Macbook.
What is Prometheus?
Prometheus is an open source metrics collection and storage platform. It's used extensively around the world, especially (in my experience) for monitoring Kubernetes platforms.
The Prometheus node exporter is a program which collects operating system level metrics such as CPU usage and exposes them via a standard /metrics endpoint. Prometheus can then "scrape" the data from this endpoint and store it.
Installing and running the Node Exporter
I wanted to install and run Prometheus and the node exporter via the terminal (because I'm still learning my way around my Macbook and wanted to learn how). You could just as easily do most, if not all of what I have explained below via the UI.
You can find the download link for the Mac version of the node exporter on the Prometheus downloads page. The Mac version of the exporter is labelled "darwin" as the OS. Once you have the download URL you can download it from a terminal using the command (replace the URL with the current version/version you want to install):
curl -O 'https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.darwin-amd64.tar.gz' -L
Then to unzip the archive:
gunzip -c node_exporter-1.4.0.darwin-amd64.tar.gz| tar xopf -
And to run it:
cd node_exporter-1.4.0.darwin-amd6
./node_exporter
You can check that it's running by opening a browser at https://127.0.0.1:9100/metrics
Installing and configuring Prometheus
You can find the download link for Prometheus at the same page linked above. You can also use the same approach via the terminal to download and unzip it:
领英推荐
curl -O 'https://github.com/prometheus/prometheus/releases/download/v2.40.1/prometheus-2.40.1.darwin-amd64.tar.gz' -L
gunzip -c prometheus-2.40.1.darwin-amd64.tar.gz| tar xopf -
Before you run Prometheus you need to add your node exporter endpoint as a "scrape config". What this means is that you tell Prometheus about a /metrics endpoint you want it to collect from, and it will "scrape" all the metrics there on a regular basis.
Open up prometheus.yml in your IDE or code editor of choice and add the following entry under scrape_configs:
- job_name: "node_your_mac"
static_configs:
- targets: ["localhost:9100"]"
Once you've made the change and saved the file you can run Prometheus:
./prometheus
Checking the scrape is working
Once Prometheus is running you can view the Prometheus UI from https://127.0.0.1:9090. If you go to Status > Targets you should see your node exporter endpoint there:
You can double check the data is there by running a query, for example, the following shows you the average % CPU usage on your Mac each minute:
100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[1m])) * 100)
You should see data coming back:
Summary
In the real world I don't think many/any people use Prometheus to monitor their laptops, but the same principles apply at bigger scale. There are node exporters for Linux and Windows hosts and Kubernetes nodes. What's great about the node exporter is that it gives you a consistent way to query node/host level metrics regardless of the specific operating system being monitored.
Prometheus can do a lot more than this, and I'm looking forward to sharing more about how to leverage it next year.
Cloud & Devops | Python | Golang | K8S | K3S | CKA | CI/CD | Automation | Prometheus/Grafana | Linux | Bash | Infra
1 年Very good article. Made my testing very simple.
Principal, Director of Design Technology @ HOK | Applied AI, Innovative Application Development & Strategies, High-performance Team Builder, Large Project Management
1 年One major issue with this .. node_exporter won't report stats on sys memory when running on Mac. Surprised I have not seen more on this - has been reported on GitHub under Issues by a user but the response was "it makes more sense to ask on the prometheus-users mailing list" which isn't very helpful. Don't get me wrong, you can obtain tons of other useful information, but mem committed, etc. is a critical piece of info (arguably more than CPU for some workloads) to have at your disposal.
DevOps and Platform Engineer | Writing stories about the humans that make software
2 年Great intro. Definitely going to be trying this out soon!
Software Engineering, Tech Media Content. DevPerfOps, Performance engineering, Observability, Testing, AI. Consulting, Education, and Entertainment.
2 年Very timely my friend. ??. I was literally just starting on a project for the SMC Journal about monitoring performance of MacOS multiple ways and this just saved me a lot of work. Keep publishing articles so I can take a vacation ??
Expert in Performance Testing & QA | Infrastructure Builder | Teacher & Mentor
2 年Monitoring your Mac with Prometheus is a great way to keep track of the performance and health of your device. By using Prometheus, you can set up alerts and notifications to alert you of any issues that may arise, such as high CPU usage or low disk space. Additionally, Prometheus provides a rich set of metrics that can be used to gain insights into the overall performance of your Mac, such as CPU and memory usage, network traffic, and more. Overall, monitoring your Mac with Prometheus can help you proactively identify and troubleshoot issues, ensuring that your device is running smoothly and efficiently.