{"id":1604,"date":"2014-01-23T22:00:03","date_gmt":"2014-01-24T03:00:03","guid":{"rendered":"http:\/\/blogs.sungeek.net\/unixwiz\/?p=1604"},"modified":"2014-01-23T22:00:03","modified_gmt":"2014-01-24T03:00:03","slug":"joyent-smartos-network-monitoring","status":"publish","type":"post","link":"https:\/\/blogs.sungeek.net\/unixwiz\/2014\/01\/23\/joyent-smartos-network-monitoring\/","title":{"rendered":"Joyent SmartOS network monitoring"},"content":{"rendered":"<p>My free trial period of my Smart Machine ended, so now I was trying to find a way to monitor my bandwidth usage on my Smart machine. There isn&#8217;t a &#8220;easy&#8221; way of doing (like logging in to the portal to look at your account) so I devised a way to do it on my own.<\/p>\n<p>The first part of it will be discussed in this post, and I will do another about how to actually view the results.<\/p>\n<p>First off the easiest way I have found to &#8220;watch&#8221; network traffic is using the kstat command. On my SmartMachine, I have 2 network interfaces, one that has the public interface on it, and one that has the private interface on it. For my purposes I am only currently watching &#8220;net1&#8221; which is the external interface.<\/p>\n<p>So the small script I have runs every 10 minutes, and logs the information in to a MySQL table. That table is defined like this:<\/p>\n<p><code lang=\"sql\">CREATE TABLE `vmnet` (<br \/>\n`interface` char(10) DEFAULT NULL,<br \/>\n`time` bigint(20) DEFAULT NULL,<br \/>\n`obytes` bigint(20) DEFAULT NULL,<br \/>\n`rbytes` bigint(20) DEFAULT NULL,<br \/>\n`htime` datetime DEFAULT NULL,<br \/>\nKEY `tidx` (`time`)<br \/>\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>The columns are as follows:<\/p>\n<ul>\n<li>interface: which interface we are getting the stats from, right now everything just says net1. But if I were to add net0 it would fit right in.<\/li>\n<li>time: time in seconds since the epoch<\/li>\n<li>obytes: bytes leaving the interface<\/li>\n<li>rbytes: bytes received on the interface<\/li>\n<li>htime: human readable time. (Yes i realize I am storing the time twice, and that I can do everything with just time, but what the heck, it is just an extra little storage ;-)&#8230;<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Now that the table in the DB is defined, set the permissions on it. In my case I created a database just for the &#8220;netstats&#8221; ?and there is just the one table in it called vmnet. I created 2 users that have access to the vmnet table. One just for writing the data in from the script, and another for reading the data for part 2 of this.<\/p>\n<p>&nbsp;<\/p>\n<p>Now for the script, it is pretty simplistic:<br \/>\n<code lang=\"bash\"><br \/>\n#!\/bin\/bash<br \/>\n#Use kstat to grab interface stats<br \/>\n#Define the interface to look at:<br \/>\nINTF=\"net1\"<br \/>\nVALUES=\"`kstat -c net -n ${INTF} | egrep \\\"(obytes64|rbytes64)\\\"`\"<br \/>\nSNAPTIME=\"`perl -e \\\"print(time());\\\"`\"<br \/>\nOBYTES=\"`echo ${VALUES} | grep obytes64 | awk '{print $2}'`\"<br \/>\nRBYTES=\"`echo ${VALUES} | grep rbytes64 | awk '{print $4}'`\"<br \/>\necho \"insert into vmnet values ('${INTF}',${SNAPTIME},${OBYTES},${RBYTES},NOW());\" | \/opt\/local\/bin\/mysql -uUUUUUU -pPPPPPPPPP netstats<br \/>\n<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>In the most?simplest?form, the script runs the kstat command on the requested interface ${INTF} and then uses egrep to grab the obytes64 and rbytes64. It then takes those to values and creates a sql insert and piles that in to mysql command where UUUUUU is the username and PPPPPPPP is the password for the insert use on the netstats database.<\/p>\n<p>I then run this every 10 minutes. And what you end up with is data in the table that looks like this:<\/p>\n<pre>+-----------+------------+------------+------------+---------------------+\r\n| interface | time       | obytes     | rbytes     | htime               |\r\n+-----------+------------+------------+------------+---------------------+\r\n| net1      | 1388373702 | 3123241114 | 3977125001 | 2013-12-29 22:21:42 |\r\n| net1      | 1388374200 | 3123381303 | 3977326242 | 2013-12-29 22:30:00 |\r\n| net1      | 1388374457 | 3140146411 | 3977725426 | 2013-12-29 22:34:17 |\r\n| net1      | 1388374800 | 3140170245 | 3977843340 | 2013-12-29 22:40:00 |\r\n| net1      | 1388375400 | 3140526526 | 3978051264 | 2013-12-29 22:50:00 |\r\n+-----------+------------+------------+------------+---------------------+<\/pre>\n<p>Next time I will show how to take the data and make something out of it:<\/p>\n<figure id=\"attachment_1609\" aria-describedby=\"caption-attachment-1609\" style=\"width: 811px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/blogs.sungeek.net\/unixwiz\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-23-at-9.54.55-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-1609 \" alt=\"graph of network traffic\" src=\"http:\/\/blogs.sungeek.net\/unixwiz\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-23-at-9.54.55-PM.png\" width=\"811\" height=\"578\" srcset=\"https:\/\/blogs.sungeek.net\/unixwiz\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-23-at-9.54.55-PM.png 1014w, https:\/\/blogs.sungeek.net\/unixwiz\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-23-at-9.54.55-PM-300x213.png 300w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><figcaption id=\"caption-attachment-1609\" class=\"wp-caption-text\">netstat output<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>My free trial period of my Smart Machine ended, so now I was trying to find a way to monitor my bandwidth usage on my Smart machine. There isn&#8217;t a &#8220;easy&#8221; way of doing (like logging in to the portal to look at your account) so I devised a way to do it on my &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blogs.sungeek.net\/unixwiz\/2014\/01\/23\/joyent-smartos-network-monitoring\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Joyent SmartOS network monitoring&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[395,412,411,396],"class_list":["post-1604","post","type-post","status-publish","format-standard","hentry","category-random-stuff","tag-joyent","tag-monitoring","tag-network-usage","tag-smartos"],"_links":{"self":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts\/1604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/comments?post=1604"}],"version-history":[{"count":6,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts\/1604\/revisions"}],"predecessor-version":[{"id":1611,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts\/1604\/revisions\/1611"}],"wp:attachment":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/media?parent=1604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/categories?post=1604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/tags?post=1604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}