{"id":1303,"date":"2023-04-15T20:17:21","date_gmt":"2023-04-16T01:17:21","guid":{"rendered":"https:\/\/www.nathanhunstad.com\/blog\/?p=1303"},"modified":"2023-04-15T20:22:21","modified_gmt":"2023-04-16T01:22:21","slug":"graphing-cpu-temps-using-coretemp-and-elasticsearch","status":"publish","type":"post","link":"https:\/\/www.nathanhunstad.com\/blog\/2023\/04\/graphing-cpu-temps-using-coretemp-and-elasticsearch\/","title":{"rendered":"Graphing CPU temps using CoreTemp and Elasticsearch"},"content":{"rendered":"\n<p>Recently I purchased a <a href=\"https:\/\/www.newegg.com\/black-phanteks-eclipse-g360a-micro-atx-mid-tower\/p\/N82E16811854116?Item=N82E16811854116\" target=\"_blank\" rel=\"noopener\" title=\"\">new computer case<\/a> to replace the <a href=\"https:\/\/www.newegg.com\/p\/N82E16811103010\" target=\"_blank\" rel=\"noopener\" title=\"\">ancient giant one<\/a> I bought in 2009. The various fans had stopped working, and I realized that I no longer had a large number of external drives like DVD drives and so forth, so when Newegg had that case on sale, I uncharacteristically made an impulse purchase.<\/p>\n\n\n\n<p>After moving everything over and getting things up and running again, I decided that I wanted to monitor the cooling performance of my new case. I have long used <a href=\"https:\/\/www.alcpu.com\/CoreTemp\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Core Temp<\/a> to show the temperature of my CPU in my system tray, but that only showed the current temp. I had no way of graphing that information over time. Since I use Elasticsearch as my logging platform, the obvious solution was to somehow get the data into Elasticsearch and show it on a Kibana dashboard. This is how I did it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Install Core Temp Remote Server<\/h2>\n\n\n\n<p>Core Temp has a number of <a href=\"https:\/\/www.alcpu.com\/CoreTemp\/addons.html\" target=\"_blank\" rel=\"noopener\" title=\"\">plugins<\/a>, and one of them is Core Temp Remote Server. It exposes data from Core Temp on a port, conveniently in JSON format. Installing and configuring this plugin was simple, and after it was running, Core Temp data was accessible on port 5200.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Set up Logstash listener<\/h2>\n\n\n\n<p>I use Logstash to ingest various logs into Elasticsearch. To get this data into Elasticsearch, all I had to do was create a new input:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  tcp {\n    host =&gt; \"127.0.0.1\"\n    port =&gt; \"5200\"\n    codec =&gt; \"json\"\n    tags =&gt; &#91; \"coretemp\" ]\n  }\n<\/code><\/pre>\n\n\n\n<p>as well as a new output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  else if \"coretemp\" in &#91;tags] {\n    elasticsearch {\n      hosts =&gt; &#91; \"127.0.0.1:9200\" ]\n      index =&gt; \"logstash-coretemp-%{+YYYY.MM.dd}\"\n      manage_template =&gt; false\n      user =&gt; logstash_writer\n      password =&gt; XXXXXXXXXXXXXXXXXXXXX\n    }\n  }\n<\/code><\/pre>\n\n\n\n<p>With that configuration, sending Core Temp data to port 5200 on the Logstash server would get it into Elasticsearch, and since it&#8217;s already in JSON format, no additional parsing would be necessary.<\/p>\n\n\n\n<p>There was one last item though: both Core Temp and Logstash had listeners for data. That meant I had to create the connection to move the data over.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Create the connection<\/h2>\n\n\n\n<p>My first thought about how to connect the two together was to use netcat on the Logstash machine, which is a Linux server. I would netcat to the Core Temp server, and pipe the output to another netcat instance to output it to Logstash:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nc 192.168.1.100 5200 | nc 127.0.0.1 5200<\/code><\/pre>\n\n\n\n<p>And it worked! However, it was running in my terminal and thus I couldn&#8217;t do anything else, and it would cease running if I disconnected my SSH session. Using nohup, disown, or tmux could solve those problems, but the other problem was that it was brittle: if either Core Temp or Logstash weren&#8217;t available, it would die and I&#8217;d have to manually restart it. No good.<\/p>\n\n\n\n<p>So I started Googling and found some potential solutions, which I ultimately combined to make it resilient: first, I&#8217;d drop netcat for socat, so as to be able to do it in a one-liner:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>socat TCP4:192.168.1.100:5200 TCP4:127.0.0.1:5200 &amp;<\/code><\/pre>\n\n\n\n<p>socat basically works like the two netcat processes I had piped together, so that was simple. I still needed a way to monitor that socat was running and restart it if it wasn&#8217;t, and Google again pointed me to the answer: create a cron script to make sure it is running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\ncase \"$(pidof socat | wc -w)\" in\n0) socat TCP4:192.168.1.100:5200 TCP4:127.0.0.1:5200 &amp;\n   ;;\n1) ;;\n*) kill $(pidof socat | awk '{print $1}')\n   ;;\nesac\n<\/code><\/pre>\n\n\n\n<p>I saved the above script to a file and set up a cron job to run every 5 minutes. Now, if socat dies because Core Temp or Logstash are unavailable, once they are back socat will be sending data within five minutes. And that&#8217;s good enough for me.<\/p>\n\n\n\n<p>With that done, all that was left was to create some dashboards:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"474\" src=\"https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/image-1024x474.png\" alt=\"Kibana dashboard\" class=\"wp-image-1304\" srcset=\"https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/image-1024x474.png 1024w, https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/image-300x139.png 300w, https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/image-768x355.png 768w, https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/image-1536x710.png 1536w, https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/image-2048x947.png 2048w, https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/image-1568x725.png 1568w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>With these dashboards, I can easily monitor temp, load, memory consumption, and other data as I see fit. Mission accomplished!<\/p>\n\n\n\n<p>Bonus picture of new case:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/PXL_20230416_010813136.jpg\"><img decoding=\"async\" src=\"https:\/\/www.nathanhunstad.com\/blog\/wp-content\/uploads\/2023\/04\/PXL_20230416_010813136.jpg\" alt=\"Side of computer case\" class=\"wp-image-1305\"\/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Recently I purchased a new computer case to replace the ancient giant one I bought in 2009. The various fans had stopped working, and I realized that I no longer had a large number of external drives like DVD drives and so forth, so when Newegg had that case on sale, I uncharacteristically made an&hellip; <a class=\"more-link\" href=\"https:\/\/www.nathanhunstad.com\/blog\/2023\/04\/graphing-cpu-temps-using-coretemp-and-elasticsearch\/\">Continue reading <span class=\"screen-reader-text\">Graphing CPU temps using CoreTemp and Elasticsearch<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[290,288,291,21,289],"class_list":["post-1303","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-core-temp","tag-elasticsearch","tag-kibana","tag-linux","tag-monitoring","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/posts\/1303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/comments?post=1303"}],"version-history":[{"count":6,"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/posts\/1303\/revisions"}],"predecessor-version":[{"id":1314,"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/posts\/1303\/revisions\/1314"}],"wp:attachment":[{"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/media?parent=1303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/categories?post=1303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathanhunstad.com\/blog\/wp-json\/wp\/v2\/tags?post=1303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}