echo "

Swap clean up script (update)

Today I had a machine that had no swap space left which in return triggered a monitoring alert. Since the pressure was already gone but the swap wasn't cleared fast enough I searched and found this script as a nice solution. The script was a bit older and was made for a version of free that seperated buffers and caches. In newer versions they are combined as "buff/cache" and a new field is introduced that shows the combined available memory. Which gave me a chance to simplify the script.

clearswap.sh

#!/bin/bash

free_mem="$(free | grep 'Mem:' | awk '{print $7}')"
used_swap="$(free | grep 'Swap:' | awk '{print $3}')"

echo -e "Free memory:\t$free_mem kB ($((free_mem / 1024)) MiB)\nUsed swap:\t$used_swap kB ($((used_swap / 1024)) MiB)"
if [[ $used_swap -eq 0 ]]; then
    echo "Congratulations! No swap is in use."
elif [[ $used_swap -lt $free_mem ]]; then
    echo "Freeing swap..."
    sudo swapoff -a
    sudo swapon -a
else
    echo "Not enough free memory. Exiting."
    exit 1
fi

 Thx Scott Severance for the initial script

Attaching a USB device to KVM that reconnects frequently

I was trying to flash a new firmware to my BT headset (Sena SMH5-FM) and found out that the device reconnects several times during the flash process. Since KVM forces the use of BUS and ID rather than VendorID:DeviceID and the ID increments by one every reconnect this never works more than one time. To work around this I made a solution using UDEV to force a connect/reconnect of the device in KVM everytime it is being connected/disconnected to/from the host.

 

First find out the name of your KVM machine, you can try this command to get only the name portion

ps -ef | grep kvm |sed -n -e 's/^.*\(-name \)/\1/p'| cut -f2 -d" "

Mine is called win2k12r2, albeit a Win10

Stop the VM and create the following file (if you already tried to flash and you have a device like mine that also changes the DeviceID a couple of times make more entries) -> replace Sena with a portion of the name or brand of your device (see output lsusb)

grep -B3 Sena /var/log/syslog | sed -n -e 's/^.*\(idProduct\)/\1/p' | sort | uniq

I got this as a result (I suppose they just change it twice and that the logic is xxxx becomes yxxx and one time they use a generic ffff)

idProduct=4d65
idProduct=cd65
idProduct=ffff

The VendorID is always the same and for Sena it is 092b

 

Next I created a USB device xml for everyone of them under

/etc/libvirt/qemu/hostdev-092b:ffff.xml

<hostdev mode='subsystem' type='usb'>
<source>
<vendor id='0x092b'/>
<product id='0xffff'/>
</source>
</hostdev>

and a UDEV rule to connect and disconnect them

/etc/udev/rules.d/10-local.rules

ACTION=="add", ATTRS{idVendor}=="092b", ATTRS{idProduct}=="ffff", RUN+="/usr/bin/virsh attach-device win2k12r2 /etc/libvirt/qemu/hostdev-092b:ffff.xml"
ACTION=="remove", ATTRS{idVendor}=="092b", ATTRS{idProduct}=="ffff", RUN+="/usr/bin/virsh detach-device win2k12r2 /etc/libvirt/qemu/hostdev-092b:ffff.xml"
ACTION=="add", ATTRS{idVendor}=="092b", ATTRS{idProduct}=="cd65", RUN+="/usr/bin/virsh attach-device win2k12r2 /etc/libvirt/qemu/hostdev-092b:cd65.xml"
ACTION=="remove", ATTRS{idVendor}=="092b", ATTRS{idProduct}=="cd65", RUN+="/usr/bin/virsh detach-device win2k12r2 /etc/libvirt/qemu/hostdev-092b:cd65.xml"
ACTION=="add", ATTRS{idVendor}=="092b", ATTRS{idProduct}=="4d65", RUN+="/usr/bin/virsh attach-device win2k12r2 /etc/libvirt/qemu/hostdev-092b:4d65.xml"
ACTION=="remove", ATTRS{idVendor}=="092b", ATTRS{idProduct}=="4d65", RUN+="/usr/bin/virsh detach-device win2k12r2 /etc/libvirt/qemu/hostdev-092b:4d65.xml"

 

After this you can start your VM and try to flash your device, if it still fails check again to see if there are no extra DeviceID's in the syslog

Combining 2 MP4 movies in to a split view movie

I installed a set of cameras on my bike and the streams are saved separately on the SD card. There is some kind of Windows tool included but I wanted to combine them on Linux, since I don't use Windows at home.

 

This ffmpeg command will take 2 streams and put them together side to side in a new MP4 file. Make sure both streams have the same resolution!

ffmpeg \
-i CAM1.MP4 \
-i CAM2.MP4 \
-filter_complex '[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]' \
-map [vid] \
-c:v libx264 \
-crf 23 \
-preset veryfast \
split-view.MP4

Shushing a syslog spammer

Today I had a machine of which the /var/log/messages was drowning in DEBUG and TRACE messages from the spring framerwork used by Tomcat. To prevent the partition from running full I made a temporary workaround until our programmer disabled the extensive debugging. I found this sort of lines in /var/log/messages:

May 21 03:38:54 tomcatserver current: 03:38:54.431 [XNIO-3 I/O-3] TRACE org.xnio.nio.selector - Selected on sun.nio.ch.EPollSelectorImpl@15e3ed9
May 21 03:38:54 tomcatserver current: 03:38:54.431 [XNIO-3 task-15] TRACE org.xnio.safe-close - Closing resource io.undertow.servlet.core.ServletBlockingHttpExchange@3168bcd7
May 21 03:38:54 tomcatserver current: 03:38:54.431 [XNIO-3 I/O-3] TRACE org.xnio.nio.selector - Beginning select on sun.nio.ch.EPollSelectorImpl@15e3ed9 (with timeout)
May 21 03:38:54 tomcatserver current: 03:38:54.433 [XNIO-3 I/O-3] TRACE org.xnio.nio.selector - Selected on sun.nio.ch.EPollSelectorImpl@15e3ed9
May 21 03:38:54 tomcatserver current: 03:38:54.434 [XNIO-3 I/O-3] TRACE org.xnio.nio.selector - Selected key sun.nio.ch.SelectionKeyImpl@e68ade3 for java.nio.channels.SocketChannel[connected oshut local=/164.35.83.148:9000 remote=/10.68.64.38:45856]
May 21 03:38:54 tomcatserver current: 03:38:54.434 [XNIO-3 I/O-3] TRACE org.xnio.listener - Invoking listener io.undertow.util.ConnectionUtils$4@1d0ccab7 on channel org.xnio.conduits.ConduitStreamSourceChannel@56407229
May 21 03:38:54 tomcatserver current: 03:38:54.434 [XNIO-3 I/O-3] TRACE org.xnio.nio - Cancelling key sun.nio.ch.SelectionKeyImpl@e68ade3 of java.nio.channels.SocketChannel[connected oshut local=/164.35.83.148:9000 remote=/10.68.64.38:45856] (same thread)
May 21 03:38:54 tomcatserver current: 03:38:54.434 [XNIO-3 I/O-3] TRACE org.xnio.listener - Invoking listener io.undertow.server.AbstractServerConnection$CloseSetter@788b3120 on channel org.xnio.nio.NioSocketStreamConnection@5ab7faac

/etc/rsyslog.d/tomcat_silencer.conf

if $programname == 'current' then ~

Where current is the name of the application that is spamming. You could also use a filter for DEBUG and TRACE messages if you like.

:msg, contains, "DEBUG" ~
:msg, contains, "TRACE" ~

Either way after you adapt the config of syslog restart and optionally run a logrotate

service rsyslog restart
logrotate --force /etc/logrotate.d/syslog
Home