Asterisk, and other worldly endeavours.

A blog by Leif Madsen

Archive for the ‘Programming’ Category

Configuring powerline to show working Git branch

So the documentation for Powerline kind of sucks. I followed this pretty good article on getting started with it. First thing I noticed however is that the if statement on the article doesn’t work if you don’t have powerline installed (which kind of defeats the purpose of having the if statement there at all).

# if powerline is installed, then use it
command -v powerline-daemon &>/dev/null
if [ $? -eq 0 ]; then
powerline-daemon -q
POWERLINE_BASH_CONTINUATION=1
POWERLINE_BASH_SELECT=1
. /usr/share/powerline/bash/powerline.sh
fi

Next up is the configuration. I primarily use my bash prompt as a way to indicate which branch I’m working in within a Git repository. You need to point at the default_leftonly theme which is pretty easy to find when you web search for it. The issue is everything seems to just point you at the powerline docs, which aren’t the most clear.

First, start by creating a local configuration directory that will override the configuration for powerline for your user.

$ mkdir -p ~/.config/powerline

Then the next thing is to copy over the config.json from the main powerline configuration directory where you can find the available color schemes and other shell, i3, vim, etc themes.

(Again, the documentation kind of sucks on where the root of these configurations live…)

On my Fedora 22 system they live in /etc/xdg/powerline/. I then copy the config.json from that directory to ~/.config/powerline

To get the Git branch stuff going, I modified the configuration file in the following way:

--- /etc/xdg/powerline/config.json 2015-02-18 18:56:51.000000000 -0500
+++ /home/lmadsen/.config/powerline/config.json 2015-09-09 17:11:43.937522571 -0400
@@ -18,7 +18,7 @@
},
"shell": {
"colorscheme": "default",
- "theme": "default",
+ "theme": "default_leftonly",
"local_themes": {
"continuation": "continuation",
"select": "select"

To make it active you can run powerline-config --reload. If you have any errors in your configuration (I actually ran into this when playing with the colorscheme setting and used “solorized” instead of “solarized”), you can check it with powerline-lint.

Written by Leif Madsen

2015/09/09 at 4:20 pm

bash creating files named ‘1’ everywhere!

So I ran into something kind of stupid today 🙂 Adding a little note for anyone who might run into a similar instance.

I have some ssh-add stuff that gets run in my .bashrc file, but when I was outputting it, I was doing:

ssh-add ~/.ssh/some_key > /dev/null 2&>1

Note the 2&>1 at the end. That means to redirect output to a file named 1. You need to flip the &> into >&, so the fixed version looks like:

ssh-add ~/.ssh/some_key > /dev/null 2>&1

Written by Leif Madsen

2012/07/19 at 10:03 am

Posted in Musings, Programming

Tagged with , , ,

Converting multiple exten => lines to using same => in Asterisk dialplan

Last week I wanted to start changing some 1.4 based Asterisk dialplan to a 1.8 based Asterisk system, and in that process wanted to convert lines like:

exten => _NXXNXXXXXX,1,NoOp()
exten => _NXXNXXXXXX,2,GotoIf($[...]?reject,1)
exten => _NXXNXXXXXX,3,Dial(SIP/foo/${EXTEN})
...

into using the same => prefix:

exten => _NXXNXXXXXX,1,NoOp()
 same => n,GotoIf($[...]?reject,1)
 same => n,Dial(SIP/foo/${EXTEN})

In order to do that, Mike King helped me out with the following regular expressing which I used in vim:

%s/exten\s*=>\s*[^,]\+,\s*[n2-9]/ same => n/g

Written by Leif Madsen

2012/01/16 at 8:28 am

Controlling outbound bitrate using tc

Today I was using the VMware vCenter Converter application to build a VM from a physical machine so that I could replace Ubuntu 10.04.3 LTS with VMware ESXi (and move the functionality that the server is performing now to a VM instead of it being the base OS).

Because my server is colocated in a friends rack, and the bandwidth is shared, I needed to limit the rate at which the data was being sent from the colocated server to the virtual machine server. I needed to do this so he didn’t get pages, and so that my local connection would remain viable for VoIP communication.

After scouring Google, I found this page: http://opalsoft.net/qos/DS.htm

I looked at the HTB queuing section, and came up with a simple rate limiter for my outbound data to a specific IP. The example he shows is more complex, but it gave me enough to make it work. Here is what I entered at the console:

tc qdisc del dev eth0 root  # clear existing rules
tc qdisc add dev eth0 root handle 1:0 htb
tc class add dev eth0 parent 1:0 classid 1:1 htb rate 2048kbit
tc class add dev eth0 parent 1:1 classid 1:2 htb rate 1228kbit ceil 2048kbit
tc class add dev eth0 parent 1:2 classid 1:21 htb rate 1228kbit ceil 2048kbit
tc qdisc add dev eth0 parent 1:21 handle 210: pfifo limit 10
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 172.16.0.228/32 flowid 1:21

iptables -t mangle -A OUTPUT --dest 172.16.0.228 -p tcp -j MARK --set-mark 21

I’m sure I could have made that a bit more efficient on lines 4 and 5, but not knowing a ton about tc and the fact it worked, made me happy enough 🙂

Written by Leif Madsen

2011/11/02 at 11:08 am

Return just PID of script with ‘ps’ and ‘awk’

Today I ran into an issue where I am running a python script that I needed to get the process ID (PID) of, but that the process was being output with a space between ‘python’ and the actual script name (in this case, jiraircbot.py).

I’m sure it’s totally overkill and there is a much easier way I didn’t find to do this, but after some scouring of The Google, I found something that works! (The purpose of this was to kill off a rogue script process each night so I could restart it.)

Here is what the output looks like with just ps aux | grep python

# ps aux | grep python
root      1120  0.0  0.2  50176  4380 ?        Sl   Aug04  24:52 /usr/bin/python /usr/bin/fail2ban-server -b -s /var/run/fail2ban/fail2ban.sock
root     18182  2.2  1.5  35328 32148 pts/0    S    08:21   0:11 python jiraircbot.py
root     18219  0.0  0.0   3328   804 pts/0    S+   08:29   0:00 grep python

A little bit more data than I wanted, plus of course ‘grep python’ is always going to be returned if I just use grep straight up. Putting many pieces together from a few websites, this is what I came up with to just return the PID of the jiraircbot.py script:

ps -eo pid,command | grep "jiraircbot.py" | grep -v grep | awk '{print $1}'

What I’m doing, is controlling what is returned, so in this case have ps just return the pid and command fields. Run that through grep to just get the script I wanted, pipe that back through grep to remove the line including grep python and then pipe that through awk to just return the first field (which would be the pid of the process I wanted).

All in all, a nice hack 🙂

Written by Leif Madsen

2011/09/15 at 7:34 am

Posted in Being Productive, Programming

Tagged with , , , , ,