Seite wählen

NETWAYS Blog

Monthly Snap November: OSMC, Icon Fonts & Jaspersoft Studio

November presented an exciting 10th OSMC, featured Jasper reporting and offered DevOps tips for sys admins, developer and tinkerer.weekly snap
Beginning with events, our 10th Open Source Monitoring Conference with many special guests took place and Daniela reported on day one and Dirk summarized the other days of the conference while Jean-Marcel gave us an overview of his two favorite presentations.
Eric then described how to create Icon-Fonts and Alexander explained how to upgrade python-driven servers.
Lastly, Christoph looked at Jaspersoft Studio and Kay shared a guide to control home automation components with Rasperry PI and a web interface.

Stephanie Kotilge
Stephanie Kotilge
Accountant

Steffi ist seit 2011 bei NETWAYS. Sie fing als Office Managerin an und unterstützt seit 2017 als Accountant das Finance & Administration Team in allen buchhalterischen Belangen. In ihrer Freizeit ist sie mit ihrem Sohn immer auf der Suche nach den schönsten Spielplätzen in Nürnberg oder plant den nächsten Familientrip.

powerline-shell

powerline-shell
Hi folks,
ich habe letztens bei meinem Kollegen Mr. Frog ein neues Bling-Bling entdeckt The Mighty Powerline-Shell die mir besonders gut gefallen hat, ich war so angefixt das Ich mir einbildete das auch haben zu müssen, gesagt getan.
Die Powerline-Shell ist richtig Gut, da Sie als Segmentiertes ’self contained‘ Python-Script folgende Features abdeckt.

  • Ihr seht egal ob Git, SVN, Mercurial o. Fossil immer was gerade Sache ist
  • wenn eure Pfade auf einer Zeile zu lang werden, dann werden Sie gekürzt ohne relevante Information zu verlieren
  • viel Buntes für den Kreativen Ops/Dev ( Colored Themes )
  • auch werden euch Rückgabe werte des letzten Befehls aus eurer Shell ungleich 0 zurückgegeben
  • wenn Ihr euch per SSH auf entfernte Systeme verbindet, dann wird auch dies durch ein Schloss Symbol kenntlich gemacht
  • wenn das aktuelle Verzeichnis nicht mit euren Rechten beschrieben werden darf, wird dies auch durch ein Schloss Symbol wiedergegeben
  • Sie kann auch den Title eures TerminalEmulator mit user@host:/path Einträgen umschreiben
  • zeigt auch Änderungen an der Umgebung an ( Virtual Environment )
  • die aktuelle Zeit ist nun auch mit am Start ( Segment wurde von mir eingeführt, da ich dieses Feature für mein Daily benötige )
  • zeigt auch laufende Hintergrund Jobs an ( Shell forks )
  • die Features sind leicht zu erweitern ( Segmente werden in Python geschrieben )

Die Powerline-Shell ist auch sehr benutzerfreundlich in etwa 3 Schritten eingerichtet.

cd ~ && mkdir git && cd git
git clone https://github.com/powerline/fonts.git powerline-shell-fonts
git clone 
./powerline-shell-fonts/install.sh
./powerline-shell/install.py
vi ~/.bashrc

Noch fix den Prompt auf die Powerline-Shell umbiegen. 😉

function _update_ps1() {
	export PS1="$(~/powerline-shell.py $? 2>> /dev/null)"
}
export PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"

Und einen der neu installierten Fonts mit dem ‚Powerline‘ Suffix wählen, fertig.
Hier noch Bilder „weil Bilder mehr als tausend Worte …“ naja ihr wisst schon.
github-lenrico-powerline-shell
Links:

https://github.com/Lokaltog/powerline-fonts
 

From Perl to Python and beyond

I’ve seen and learned plenty of programming languages either during my studies or in work or spare time related projects – be it C/C++/C#, VHDL, Java or PHP/Perl/Python/Ruby even (I’ve removed some in my XING profile to reduce recruiter spam level ;)). Choosing the „right“ language is always hard but most of the time the requirements of existing software or newly designed projects allow you to skip that part and already have one in mind.
python-logo-master-v3-TMWhen joining NETWAYS in late 2012, I took over the LConf backend project being entirely written in Perl similar to other plugins and scripts floating around as open source software. Icinga 1.x Core is partially using Perl as well (although embedded Perl in C is a horrible mess). Most recently our development team decided to go for Python as the primary programming language.
So, my Python foo wasn’t that good after some years not really using it. Learning from my colleagues and looking into additional ressources helped a lot. I also found „Head first Python“ from O’Reilly in my bookshelf as a gift from the Nagios/Icinga cookbook review which also provides an extensive introduction into Python – when you already know a scripting language like Perl.
We’ve been working on a customer’s project developing a plugin framework for executing checks via a defined transport (e.g. ssh) and parsing cli output. Find some collected hints and tricks I’ve learned below.

Remove colors from shell output

The EMC „isi“ cli command puts colors into the shell output (e.g. for a critical state turning the background red). While one could disable color support on the shell, this was not possible in that environment. The fix by Gunnar was easy: Parsing text output into a list called „lines“ and clean the line string from shell color codes:

lines = text.split('\n')
lines = [re.sub('\x1b\[[0-9;]*m', '', line).strip() for line in lines]

Convert datetime to unix timestamp

Consider having a datetime string whose output format is not fixed, which means some strftime print magic does not work for proper parsing. Luckily there’s a Python module called dateutil providing the required functionality.

import time
import datetime
import dateutil.parser
def datetime2unixts(time_str):
    dt = dateutil.parser.parse(time_str)
    return time.mktime(dt.timetuple())
$ python
Python 2.7.8 (default, Apr 15 2015, 09:26:43)
[GCC 4.9.2 20150212 (Red Hat 4.9.2-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time, datetime, dateutil.parser
>>> def datetime2unixts(time_str):
...     dt = dateutil.parser.parse(time_str)
...     return time.mktime(dt.timetuple())
...
>>> time_str = "Wed May 13 16:15:34 CEST 2015"
>>> datetime2unixts(time_str)
1431526534.0

Initialize nested dictionary

Similar to what I am used to do with Perl and hashes I was stumbling over this with Python. In Perl one does not care about initializers, but just keeps populating all items like so:

my $hash = {}
$hash{'key1'}{'key2'} = "val1";

In Python this does not work out-of-the-box. There are some modules available such as „collectors“, but I prefer a somewhat native implementation making it work on older distributions. I came across this solution which I consider pure awesome 🙂

class AutoVivification(dict):
    """Implementation of perl's autovivification feature."""
    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            value = self[item] = type(self)()
            return value
hash = AutoVivification()
hash["key1"]["key2"] = "val1"

Note: It certainly does create non-existing keys if you are checking for their existance. My implementation does not care about key checks, but requires known-to-exist values from a flat dictionary with „id_label“ as key stashed into a nested dictionary where id and label are separated/nested for further operations.

        jobs = AutoVivification()  # use a pre-initialized nested dict
        for k, v in sorted(perfdata.iteritems()):
            (job_id, label) = k.split("_", 1)
            if label == "ended_ts":
                jobs[job_id][label] = float(v)
            if label == "started_ts":
                jobs[job_id][label] = float(v)

Fix the indent

If your editor doesn’t do that automatically (e.g. converting tabs into 4 spaces) it might still work, but could also cause weird behaviour in Python depending on the indent only (no brackets as I am used to with Perl). Fixing this is fairly easy by using autopep8 – I’m using Fedora 22:

dnf install python-autopep8
autopep8 check_plugin -i

Note: ‚-i‘ replaces the given file with all the changes. Make sure to commit your other changes to git before.

Conclusion

I never thought that I would dig deep in Python again that easy. I used to hack that in the past with Win2k usb driver test frameworks, and also checkmk plugins, but gaining back experience worked out pretty well.
Icinga 2’s code, value types and configuration look a lot like Python as well – take dictionaries for custom attributes using apply for loops, eh? (Hint: Testdrive this config snippet inside the Icinga 2 Docker container).

object Host "dns" {
  import "generic-host"
  address = "127.0.0.1"
  address6 = "::1"
  vars.dns_checks["dns icinga.org"] = {
    dns_lookup = "icinga.org"
    dns_server = "ns1.netways.de"
    dns_expected_answers = "185.11.254.83"
  }
  vars.dns_checks["dns netways.org"] = {
    dns_lookup = "netways.org"
    dns_server = "ns1.netways.de"
    dns_expected_answers = "185.11.252.37"
  }
}
apply Service for (dns_check => config in host.vars.dns_checks) {
  check_interval = 1m
  retry_interval = 30s
  check_command = "dns"
  vars += config
}

Icinga 2, plugins, your project – here I come 🙂
PS: See you at the OSMC hackathon.

OSMC 2014: Der Countdown läuft – nur noch 120 Tage

Mit Georg Kostner geht es im heutigen Video-Countdown um Al’exa.

OSMC? Was soll das denn sein und wer sind die netten Menschen in diesen Videos? Die Open Source Monitoring Conference (kurz: OSMC) ist die internationale Plattform für alle an Open Source Monitoring Lösungen Interessierten, speziell Nagios und Icinga. Jedes Jahr gibt es hier die Möglichkeit sein Wissen über freie Monitoringsysteme zu erweitern und sich mit anderen Anwendern auszutauschen. Die Konferenz richtet sich besonders an IT-Verantwortliche aus den Bereichen System- und Netzwerkadministration, Entwicklung und IT-Management. Und die netten Menschen, die Ihr in unseren Videos zur OSMC seht, gehören dazu. 2014 wird die OSMC zum 9. Mal in Nürnberg stattfinden.

Weekly Snap: OSDC 2014, Puppet Camp & GnuPG/GPG Best Practices

weekly snap7 – 11 April was OSDC 2014 and PuppetCamp week, with a few GPG best practices and Python musings slipped in.
Eva ended her countdown to the OSDC with Daniel Kirstenpfad’s talk on ‘Why Virtual Development and Testing isn’t Rocket Science’ and thanked all attendees and sponsors in advance. Dirk and Michael then followed with their highlights from the first and second days at the OSDC 2014.
Continuing with events, Christian announced the next round of webinars featuring Request Tracker, Puppet, Bareos and inGraph and Dirk returned with his review of Puppet Camp 2014.
Thomas then gave a comprehensive rundown of GnuPG / GPG best practices as Alexander shared his thoughts on Python 2.4 and RHEL.
Finally, Georg ended the week by introducing the newest addition to our hardware store: HW Group Poseidon2 3266.