{"id":138,"date":"2015-04-26T17:46:17","date_gmt":"2015-04-26T16:46:17","guid":{"rendered":"https:\/\/zorq.net\/b\/?p=138"},"modified":"2015-04-26T17:46:17","modified_gmt":"2015-04-26T16:46:17","slug":"getting-started-with-graylog-on-macos","status":"publish","type":"post","link":"https:\/\/zorq.net\/b\/2015\/04\/26\/getting-started-with-graylog-on-macos\/","title":{"rendered":"Getting Started with Graylog on MacOS"},"content":{"rendered":"<p><a href=\"https:\/\/www.graylog.org\/\">Graylog<\/a> is an amazing open source tool for recording, viewing and analysing application logs. The performance and full text indexing of the underlying <a href=\"https:\/\/www.elastic.co\/products\/elasticsearch\">elasticsearch database<\/a> means you probably won&#8217;t need any other logging tools throughout your whole organisation.<\/p>\n<p>Application developers writing to Graylog may need a local instance to retrieve diagnostic information during development. This post gives some quick instructions for setting up a local logging server using docker.<\/p>\n<p><a href=\"https:\/\/www.docker.com\/\">Docker<\/a> is used for managing lightweight operating system containers with isolated process and network namespaces &#8211; each container can have a process of PID 10 using TCP port 80. Unfortunately, Mac OS X&#8217;s kernel doesn&#8217;t contain the necessary APIs to run a Linux-based container and so we need to use a Virtual Machine running Linux itself. <a href=\"https:\/\/github.com\/boot2docker\/boot2docker\">Boot2docker<\/a>, installed via Homebrew, comes to our rescue to make the whole process as simple as possible:<\/p>\n<pre lang=\"bash\">\r\nbrew install Caskroom\/cask\/virtualbox\r\nbrew install Caskroom\/cask\/boot2docker\r\nbrew install docker\r\n<\/pre>\n<p>Now all the tools are installed we need to download and start up the Linux VM:<\/p>\n<pre lang=\"bash\">\r\nboot2docker download\r\nboot2docker init\r\nboot2docker start\r\n<\/pre>\n<p>Another complexity of the docker host being in a virtual machine is that the docker command line interface needs to connect across a virtual network interface. Boot2docker helps us here by telling us the required settings. I put the &#8216;export&#8217; lines in my <tt>~\/.profile<\/tt> so that the <tt>docker<\/tt> command will work without any special setup in the future.<\/p>\n<pre lang=\"bash\">\r\nboot2docker shellinit\r\n# outputs:\r\nWriting \/Users\/djb\/.boot2docker\/certs\/boot2docker-vm\/ca.pem\r\nWriting \/Users\/djb\/.boot2docker\/certs\/boot2docker-vm\/cert.pem\r\nWriting \/Users\/djb\/.boot2docker\/certs\/boot2docker-vm\/key.pem\r\n    export DOCKER_HOST=tcp:\/\/192.168.59.103:2376\r\n    export DOCKER_CERT_PATH=\/Users\/djb\/.boot2docker\/certs\/boot2docker-vm\r\n    export DOCKER_TLS_VERIFY=1\r\n<\/pre>\n<p>To test your new docker installation you can run the <tt>hello-world<\/tt> container:<\/p>\n<pre lang=\"bash\">\r\ndocker run hello-world\r\n<\/pre>\n<p>Assuming that went okay we can now get started with Graylog! Run the following command to download the image for containers we&#8217;ll create. This may take some time as there are a few hundred MB of Linux distribution and Java libraries to fetch.<\/p>\n<pre lang=\"bash\">\r\ndocker pull graylog2\/allinone\r\n<\/pre>\n<p>You can now create a new container. The numeric arguments are the TCP ports that will be exposed by the container.<\/p>\n<pre lang=\"bash\">\r\ndocker run -t -p 9000:9000 -p 12201:12201 graylog2\/allinone\r\n<\/pre>\n<p>Once the flurry of activity in your console comes to a stop the container is fully up and running. You can visit the admin page by visiting <tt>http:\/\/ip-of-server:9000<\/tt>. The IP address is that of your boot2docker virtual machine, which you found earlier using <tt>boot2docker shellinit<\/tt>. The default username and password to the web interface are <tt>admin<\/tt> and <tt>admin<\/tt>.<\/p>\n<p>Once you&#8217;re inside the web interface Graylog will warn you that there are no inputs running. To fix this, browse to <tt>http:\/\/ip-of-server:9000\/system\/inputs<\/tt> and add an input of type <tt>GELF HTTP<\/tt> running on port <tt>12201<\/tt>. The name of the input isn&#8217;t important unless you have plans for hundreds of them so I&#8217;ve unimaginatively called mine <tt>GELF HTTP<\/tt>.<\/p>\n<p>Now we&#8217;ve got a running server configured enough for testing, but no logs! A real application will produce logs of its own but for the purpose of demonstration we can write a script to read in your local computer&#8217;s system log and send the messages to Graylog for indexing. Note that you&#8217;d never do this in production as Graylog is perfectly capable of using the syslog UDP protocol, which would avoid the need to write any code.<\/p>\n<pre lang=\"python\">\r\n#!\/usr\/bin\/env python\r\n\r\nimport urllib2\r\nimport json\r\n\r\npath = \"\/var\/log\/system.log\"\r\nwith open(path) as log:\r\n    log = f.readlines()\r\n\r\nfor line in f:\r\n    message = {}\r\n\r\n    message['version'] = '1.1'\r\n    message['host'] = 'localhost'\r\n    message['short_message'] = line\r\n\r\n    req = urllib2.Request('http:\/\/192.168.59.103:12201\/gelf')\r\n    req.add_header('Content-Type', 'application\/json')\r\n    response = urllib2.urlopen(req, json.dumps(message))\r\n<\/pre>\n<p>Once your logs are in the server you can start searching them. Try querying for <tt>kernel<\/tt> to find all the messages logged from the Mac OS X kernel. Having a full text index of our logs is useful but belies the promise of elasticsearch&#8217;s structured storage. A more useful implementation would log application-specific information as fields in the GELF message. For example, with the following code a single click a complete history of logs for a particular client could be retrieved:<\/p>\n<pre lang=\"python\">\r\nmessage['client_ip'] = ipFromApplicationVariable\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Graylog is an amazing open source tool for recording, viewing and analysing application logs. The performance and full text indexing of the underlying elasticsearch database means you probably won&#8217;t need any other logging tools throughout your whole organisation. Application developers writing to Graylog may need a local instance to retrieve diagnostic information during development. This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[20,19],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-macos","tag-devops","tag-logging"],"_links":{"self":[{"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":18,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":156,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/posts\/138\/revisions\/156"}],"wp:attachment":[{"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}