Python: Everything is an Object & First Class Citizens

A lot of Python books often mention that “everything in Python is an object”, and “objects are first class citizens”, but they don’t always explain what that these things actually mean. Let’s try to fix that up now.

Everything in Python is an Object

Dive Into Python gives a great explanation:

Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser; some objects have neither attributes nor methods (more on this in Chapter 3), and not all objects are subclassable (more on this in Chapter 5). But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function (more in this in Chapter 4).

This is so important that I’m going to repeat it in case you missed it the first few times: everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Even modules are objects.

Continue reading

Python: if __name__ == “__main__”

There are plenty of articles on the internet that attempt to explain what  if __name__ == "__main__" is and what it does, but (in my humble opinion), the examples are too complex more often than not. With that in mind, this post is aimed at being the most simplest explanation on the planet! :)

What does it do?

This statement is used when you want your code to be used as both a standalone script, as well as a module that can be imported and used by other scripts.

For example, if it is run as a standalone script, you may want to provide a menu to ensure users input all of the necessary information. On the other hand, if it is being imported as a module, perhaps you’d like to avoid the menu all together and instead only use the functions contained the script (e.g func1 in the example below).

Continue reading

Getting to know Telegraf

I first mentioned Telegraf in the My Monitoring Journey: Cacti, Graphite, Grafana & Chronograf post and then covered its installation and setup in the Installing & Setting up InfluxDB, Telegraf & Grafana post. Let’s now delve a little deeper, shall we?

The good news is that there’s a lot less to Telegraf’s configuration than what there is to InfluxDB so you’ll likely find this post easier to follow than the Getting to know InfluxDB and article.

What is it?

Before diving into configurations, it would be best to first cover off what Telegraf actually is. To quote the Telegraf GitHub page:

Telegraf is an agent written in Go for collecting, processing, aggregating, and writing metrics.

Design goals are to have a minimal memory footprint with a plugin system so that developers in the community can easily add support for collecting metrics from well known services (like Hadoop, Postgres, or Redis) and third party APIs (like Mailchimp, AWS CloudWatch, or Google Analytics).

Continue reading

Navigating InfluxDB CLI

I’ve demonstrated a few InfluxDB commands in my Getting to know InfluxDB and InfluxDB: Retention Policies & Shard Groups posts but though it would be a good idea to write a post completely dedicated to useful CLI commands – so here it is.

SHOW DATABASES

This command is self explanatory. It lists all of your InfluxDB databases:

USE <DATBASE_NAME>

Enters a database so that subsequent commands will be run against it:

Continue reading

InfluxDB: Retention Policies & Shard Groups

Note: This is a continuation of the Getting to know InfluxDB post. If you haven’t read it yet, I suggest you do before reading this post.

I found InfluxDB’s documentation around Retention Policies (RP) and Shard Groups quite unclear in parts and am therefore writing this post to assist others who find themselves feeling the same way.

What is a Retention Policy?

As the documentation says:

The part of InfluxDB’s data structure that describes for how long InfluxDB keeps data (duration), how many copies of those data are stored in the cluster (replication factor), and the time range covered by shard groups (shard group duration). RPs are unique per database and along with the measurement and tag set define a series.

When you create a database, InfluxDB automatically creates a retention policy called autogen with an infinite duration, a replication factor set to one, and a shard group duration set to seven days. See Database Management for retention policy management.

Continue reading

Getting to know InfluxDB

I touched on InfluxDB in the My Monitoring Journey: Cacti, Graphite, Grafana & Chronograf post and then covered its installation and setup in the Installing & Setting up InfluxDB, Telegraf & Grafana post. Now it’s time to look at how the database actually works and commands we can use to integrate it.

InfluxDB Structure

In the latter mentioned post above we saw that Telegraf had created a telegraf  database in InfluxDB. Let’s now jump into InfluxDB and take a look at this database:

To view a list of all of the databases, issue the following show databases  command:

Continue reading

Installing & Setting up InfluxDB, Telegraf & Grafana

I mentioned these tools in the My Monitoring Journey: Cacti, Graphite, Grafana & Chronograf post and thought now would be a good time to cover their installation and setup. Let’s get started.

Installing InfluxDB & Telegraf

Instructions on how to install all of the TICK stack components can be found here. As I’m running Ubuntu, I’ll need to run these commands:

wget https://dl.influxdata.com/influxdb/releases/influxdb_1.2.4_amd64.deb
sudo dpkg -i influxdb_1.2.4_amd64.deb
wget https://dl.influxdata.com/telegraf/releases/telegraf_1.3.1-1_amd64.deb
sudo dpkg -i telegraf_1.3.1-1_amd64.deb

Note that after running the last command, the Telegraf service automatically starts:

will@ubuntu:/tmp$ sudo dpkg -i telegraf_1.3.1-1_amd64.deb
Selecting previously unselected package telegraf.
(Reading database ... 177117 files and directories currently installed.)
Preparing to unpack telegraf_1.3.1-1_amd64.deb ...
Unpacking telegraf (1.3.1-1) ...
Setting up telegraf (1.3.1-1) ...
Created symlink from /etc/systemd/system/multi-user.target.wants/telegraf.service to /lib/systemd/system/telegraf.service.

Continue reading

Looping through Dictionaries in Ansible

Note that this post uses NTC-Ansible. Installation instructions can be found here.

In the Understanding Ansible Output Structure post we saw how to extract a single entry out of a dictionary. We saw that it’s possible to do it using the map() filter too. However, what if we want to extract all entries in the dictionary, how would we go about that? The answer is simple – use with_dict . For example, this Playbook:

Continue reading