DevOps in practice

DevOps combines Development  i.e. software engineering / coding AND Operations which is more of network / systems engineering. Although a network engineer is not supposed to completely turn into a software engineer or coder but it certainly need network engineer to gain coding skills to some extent (the more, the better) on top of his/her traditional skill set. So definitely, coding skill is a MUST for a DevOps professional just as networking or systems skills have been so far.

Below are a few practical DevOps problems along with their solution. Though these are small code snippets, it will require good basic understanding of both dev (coding/programming) & ops (systems) domain topics to solve them.  We will see what all concepts come in play to arrive on the solution to give a fair understanding of these topics and skills required. You can write the script in any programing language. However the following examples use Python which is widely accepted as de-facto coding language for network automation.

*********************************************

Problem-1, Visitors on my website: An application generates a log file (in plain text) to register hits coming on a Linux web server. This log file is created in a way that it registers the IP address of the source in plain text, with each source IP address entry in one line of the text file. There is a requirement to gather some basic statistics for our security team from this log file. They want to see top N (e.g. top 10) source IP’s from where this web server is getting most number of hits. Write a script to accomplish this task. The output should be printed on the screen in the following format:

<source_IP_address><space><count of hits>         (for top N source IP’s. One in each line of the output)

Save the script on the server as executable file & do not add any extension to the file.

A real example output would look like this:

112.32.4.1           22

200.23.3.4           19

12.1.1.2               12

112.32.4.1           7

Problem-2, Unique filename finder: On a host PC, we want to find out all the unique filenames inside a given directory. Write a script to do this with the following conditions met:

  • File names must be unique
  • File names must be sorted alphabetically
  • Exclude sub-directories & do not recurse into sub-directories
  • Do not print the path and also do not print the extension. Just the filename.
  • Print error if no directory name is passed to the script or if we pass a name that’s not a directory

Once script is developed, save the file as a executable file without any extension

Problem-3, Pack number of reservations: As train tickets are booked, a monitoring system receives a list of booked train numbers as comma separated positive integers. A data analyst is only interested in adjacent duplicate train entries. We want to prepare a script for this. So we need to pack all two or more same adjacent train numbers. Output should be printed on the screen in the following format:

<interger_value>:<number_of_times_it_appears_consicutively>

Where integer_value is the train number and after colon, it should print the number of times same train number appears consecutively in the series of train numbers

e.g.

train_number_list = 28, 30, 45, 28, 28, 40, 40, 40, 45, 678, 567, 555, 555, 555, 555, 6, 8, 23, 34, 34, 90

expected output = 28, 30, 45, 28:2, 40:3, 45, 678, 567, 555:4, 6, 8, 23, 34:2, 90

*********************************************

These are real life challenges supposed to be solved by a DevOps engineer in a day to day work routine. Solving these three problems will test your knowledge on many fundamental essentials of coding & a bit of systems administration including:

  • Basic for, if & while loops. Nested loops
  • List and dictionary usage and properties
  • Making use of dictionary for count frequency
  • Reading from a file programmably
  • Lamba function
  • Interfacing with the underlay operating system using os library
  • Navigating Linux file system
  • Converting code file to executable / script on Linux **
  • Changing file permissions on Linux **

There are multiple ways to solve a problem & no answer is better or worse than others. Of course there are efficient & recommended ways of writing a code, however at this point when you are upskilling & learning, just try to get the problem solved anyhow. It’s more than enough. It will take a lot of time and practice to improve upon & get to the next level of coding skills. It’s then you will learn and understand brute force vs sophisticated algorithm, time complexity, order of growth of your code etc. and will eventually start writing professional grade efficient code.

So below are solution to the above three problems. Problem#3 is the most complex amongst this lot. So I have listed 3 out of many different possible ways to solve it.

There is a lot to learn in DevOps like version control, CI/CD pipeline, testing, monitoring, config management etc. However coding skill is the core and fundamental skill especially for a beginner or folks from non-programming background or network/systems engineers. So if you could solve these three problems on your own within an hour or two, consider yourself DevOps ready ! At least from the coding fundamental skills prospective. If however not, no worries, learn and practice more. Sooner or later you will reach there ! A good way to learn and practice coding skills is to use online learning platforms like HackerRank (https://www.hackerrank.com/) & Leetcode (https://leetcode.com/). Most of them are absolutely free ! Just create an account & start practicing. Happy Learning !!

[ ** To make a python file run as a script (without using prefix python or python3 in the command line), we need to do a couple of things:

1) Start the script with the following line:

#!/usr/bin/env python

where “#!” is called hashbang or sha-bang or shebang and is then followed by absolute path to the python executable, e.g. “/usr/bin/env python”. This first hash bang line tells the OS that it’s a python script & locates the python executable using the path given after hash bang ( #! )

2) We may also have to change permissions on the file to make it executable based on the privileges of the user. Use the following Linux command for that:

“chmod +x <script filename with full path>”

]

Leave a comment