Category Archives: Tips & Tricks

Swap Alt and Windows keys with xmodmap?

The problem: I have a Mac keyboard where the Alt/Win (i.e. Option/Command) keys are inverted compared to a regular PC keyboard, and I'd like to swap them.

The answer:

[codesyntax lang="bash"]

# clear all options
setxkbmap -model "pc105" -layout "us,se" -option ""  

# set the Apple keyboard
setxkbmap -rules "evdev" -model "pc105" -layout "us,se" -option "terminate:ctrl_alt_bksp,lv3:rwin_switch,grp:shifts_toggle,altwin:swap_lalt_lwin"

[/codesyntax]

 

And another one which I call when I'm back on a normal keyboard:

[codesyntax lang="bash"]

# clear settings
setxkbmap -model "pc105" -layout "us,se" -option ""

# pc keyobard
setxkbmap -rules "evdev" -model "pc105" -layout "us,se" -option "terminate:ctrl_alt_bksp,lv3:rwin_switch,grp:shifts_toggle"

[/codesyntax]

VMware modules, Arch Linux & kernel 4.8.13

After upgrading the kernel to 4.8.13-1-ARCH some of the vmware kernel modules failed to compile:

/tmp/modconfig-6BT70S/vmmon-only/linux/hostif.c:1592:47: error: ‘NR_ANON_PAGES’ undeclared (first use in this function)
/tmp/modconfig-BBuLH6/vmnet-only/netif.c:468:7: error: ‘struct net_device’ has no member named ‘trans_start’; did you mean ‘mem_start’?

The fix:

[codesyntax lang="bash"]

cd /usr/lib/vmware/modules/source
tar xf vmnet.tar
tar xf vmmon.tar
mv vmnet.tar vmnet.old.tar
mv vmmon.tar vmmon.old.tar
sed -i -e 's/dev->trans_start = jiffies/netif_trans_update(dev)/g' vmnet-only/netif.c
sed -i -e 's/unsigned int anonPages = global_page_state(NR_ANON_PAGES);/unsigned int anonPages = global_page_state(NR_ANON_MAPPED);/g' vmmon-only/hostif.c
tar cf vmnet.tar vmnet-only
tar cf vmmon.tar vmmon-only
rm -r vmnet-only
rm -r vmmon-only

vmware-modconfig --console --install-all

[/codesyntax]

Be notified when critical battery level is reached

Ever happened to be focused on something and miss the fact that your laptop is running out of battery? And to lose your work?
Yesterday happened twice! "Really? Hmm... I need to fix this as soon as possible."
I googled a bit and this stackexchange post  popped up. Nice, great! But being notified even when you're charging your laptop it's not nice and I had to change the solution a bit:

[codesyntax lang="bash"]

cat .notify-send_setup

[/codesyntax]

#!/bin/bash
touch $HOME/.dbus/Xdbus
chmod 600 $HOME/.dbus/Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus

exit 0

[codesyntax lang="bash"]

cat .battnotif

[/codesyntax]

#!/bin/bash
export DISPLAY=:0
XAUTHORITY=$HOME/.Xauthority

if [ -r "$HOME/.dbus/Xdbus" ]; then
    . "$HOME/.dbus/Xdbus"
fi

battery_level=$(acpi -b | grep -P -o '[0-9]+(?=%)')
STATUS=$(acpi -b | awk '{print $3'} | sed -e 's/,//g')

if [ $battery_level -le 15 ] && [ $STATUS == "Discharging" ]
then
    /usr/bin/notify-send -u critical "Battery low" "Battery level is ${battery_level}%!"
    echo 'batt low' >> $HOME/cron.log
fi

echo 'ran batt' >> $HOME/cron.log

How to set up multiple monitors in linux (using xrandr)

If you would need to set up your displays once every week or less using the GUI is just fine. I had to do it every morning and after a while it became really annoying.

Turn off the HDMI display:
[codesyntax lang="bash"]

xrandr --output HDMI1 --off

[/codesyntax]

Turn on the HDMI display and set it as primary display:
[codesyntax lang="bash"]

xrandr --output HDMI1 --mode 1920x1080 --primary --left-of eDP1

[/codesyntax]

Marvel indices taking lot of space? Delete indices older than 7 days!

It looks like Marvel is generating some data everyday. Is there a way to reduce the amount of data generated by marvel? The short answer to the above question is Yes!

[codesyntax lang="bash"]

curator --host 127.0.0.1 show indices --older-than 30 --time-unit days --timestring '%Y.%m.%d' --prefix .marvel
curator --host 127.0.0.1 close indices --older-than 30 --time-unit days --timestring '%Y.%m.%d' --prefix .marvel
curator --host 127.0.0.1 delete indices --older-than 30 --time-unit days --timestring '%Y.%m.%d' --prefix .marvel

[/codesyntax]

If we want to remove all indices from February 2015:

[codesyntax lang="bash"]

curator --host 127.0.0.1 show indices --regex '\.marvel-2015\.02\.*'
curator --host 127.0.0.1 close indices --regex '\.marvel-2015\.02\.*'
curator --host 127.0.0.1 delete indices --regex '\.marvel-2015\.02\.*'

[/codesyntax]

Kill all mysql queries having query time greater than 1 minute

At this point there are two approaches to achieve this. One is using pt-kill from Percona Toolkit, and the other one is to use a bash script with a lot of pipes :)
Why would someone use the second approach? I don't know, perhaps because there is no Percona Toolkit available.

[codesyntax lang="bash"]

for i in $(mysql -e "show processlist" | egrep -v "system user" | grep '^[0-9]' | awk '{print $6" "$1}' | sort -nr -k1 | awk -v threshold="60" '$1 > threshold' | awk '{print $2}'); do mysql -e "kill $i"; done

[/codesyntax]

If you can/want to use pt-kill and don't know how, please read this.

Which processes are swapping?!

Recently one machine was swapping. No free space on the swap partition. Hmmm... Since this machine was used as a database server, obviously I suspected mysql. But why to guess when I could knew for sure which process was swapping...

[codesyntax lang="bash"]

for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | less

[/codesyntax]

Wasn't mysql but collectd btw :)

Access HP's ILO remote console via SSH

It happened many times to need a quick access to the remote console of a server like HP's ILO and not be able to open a web browser just to access it.
So let access it via SSH. Here goes.

[codesyntax lang="bash"]

ssh ilo_admin@ip_address

[/codesyntax]

2. Enter your ILO admin account and password. After that you will see the ILO prompt.

hpILO->

3. To access the remote console of the server at the ILO prompt type "TEXTCONS"

hpILO->TEXTCONS

4. You will be presented with the Login: console. Enter your root or user account of the server to gain access.

Login:

Voilà!

Check and mark badblocks on ext4 partitions

My storage is acting weird today and I'm trying to fix it with this command:

[codesyntax lang="bash"]

fsck.ext4 -vcDfty -C 0 /dev/vg0/lv0

[/codesyntax]

And the result was:

/dev/vg0/lv0: ***** FILE SYSTEM WAS MODIFIED *****

        6329 inodes used (0.01%, out of 107380736)
          44 non-contiguous files (0.7%)
           4 non-contiguous directories (0.1%)
             # of inodes with ind/dind/tind blocks: 0/0/0
             Extent depth histogram: 6123/119
    86511679 blocks used (20.14%, out of 429497344)
         178 bad blocks
          40 large files

        5522 regular files
         719 directories
           0 character device files
           0 block device files
           0 fifos
  4294967278 links
           0 symbolic links (0 fast symbolic links)
           0 sockets
------------
        5965 files
Memory used: 676k/416k (284k/393k), time: 21291.73/25.22/ 0.17
I/O read: 89MB, write: 19MB, rate: 0.01MB/s

178 bad blocks marked!

Check if an IP is in a subnet

At some point I counted my DROP rules in my firewall and the result was kinda frightening. A lot of subnets and even more IPs...
What was really annoying was that there were a lot of IP addresses which belonged to an already blocked subnet, so I needed a script to check this for me.

It has to be a script to do this already out there in the wild. Also a machine is faster than a human. Having this in mind, why should I reinvent the wheel? So after searching a little bit on web, I found this nice perl script.

[codesyntax lang="perl"]

#!/usr/bin/perl

use strict;

use Socket qw( inet_aton );

sub ip2long($);
sub in_subnet($$);

my $ip = $ARGV[0];
my $subnet = $ARGV[1];

if( in_subnet( $ip, $subnet ) )
{
	print "It's in the subnet\n";
}
else
{
	print "It's NOT in the subnet\n";
}

sub ip2long($)
{
	return( unpack( 'N', inet_aton(shift) ) );
}

sub in_subnet($$)
{
	my $ip = shift;
	my $subnet = shift;

	my $ip_long = ip2long( $ip );

	if( $subnet=~m|(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$| )
	{
		my $subnet = ip2long( $1 );
		my $mask = ip2long( $2 );

		if( ($ip_long & $mask)==$subnet )
		{
			return( 1 );
		}
	}
	elsif( $subnet=~m|(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/(\d{1,2})$| )
	{
		my $subnet = ip2long( $1 );
		my $bits = $2;
		my $mask = -1<<(32-$bits);

		$subnet&= $mask;

		if( ($ip_long & $mask)==$subnet )
		{
			return( 1 );
		}
	}
	elsif( $subnet=~m|(^\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})$| )
	{
		my $start_ip = ip2long( $1.$2 );
		my $end_ip = ip2long( $1.$3 );

		if( $start_ip<=$ip_long and $end_ip>=$ip_long )
		{
			return( 1 );
		}
	}
	elsif( $subnet=~m|^[\d\*]{1,3}\.[\d\*]{1,3}\.[\d\*]{1,3}\.[\d\*]{1,3}$| )
	{
		my $search_string = $subnet;

		$search_string=~s/\./\\\./g;
		$search_string=~s/\*/\.\*/g;

		if( $ip=~/^$search_string$/ )
		{
			return( 1 );
		}
	}

	return( 0 );
}

[/codesyntax]

Source: http://www.mikealeonetti.com/wiki/index.php?title=Check_if_an_IP_is_in_a_subnet_in_Perl