Category Archives: Scripts

Sysdig as a systemd service

Ran into an issue with a firewall on my CentOS7 system. In an attempt to diagnose the problem I wanted to gather as much info as possible.

A great tool to do so is sysdig https://github.com/draios/sysdig Sysdig can be considered the tcpdump for linux systems, providing a data capture of all low level system operations. In the past I had only run sysdig as a process, capturing events as I replicate an issue.

In this current case, my issue was occurring on reboot and was related to the firewall service. In order for sysdig to capture the event data for that slice of time, I had to enable it to run and capture at boot. To do so I created the following systemd service located in /etc/systemd/system/sysdig.service


[Unit]
Description=sysdig Service
After=network-pre.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/sysdig -w /var/log/sysdig.dump
Restart=on-abort

[Install]
WantedBy=multi-user.target

Resource: http://www.freedesktop.org/software/systemd/man/

The advantages to having a sysdig service

To troubleshoot on boot I can run:
sudo systemctl enable sysdig

After reboot I can stop sysdig and disable it from running on my next boot

systemctl stop sysdig
systemctl disable sysdig

The dump file is saved to /var/log

sysdig -r /var/log/sysdig.dump

This is just a starting point and can be taken much further; such as:

1. reading in a sysdig.conf file to allow for setting up a capture filter
2. versioning dump files
3. scheduling to prevent enormous captures on reboot

Code added to github: https://github.com/mdevans1/scripts/blob/master/sysdig.service

Dropzone Script for Quick File Organization

I am guilty of using my downloads folder for a catch all of random unorganized files. A folder cluttered up with ISOs PDFs DMGs and a multitude of others.

So wrote a quick dropzone script that will help me at least organize a folder of random files. I didn’t need it to be sophisticated, I just wanted to gather all files of a certain extension together.

It only takes one extension at a time, but in the future could be modified to take multiple, or maybe read from a config file for autosorting

BEFORE

Screen Shot 2015-02-06 at 8.13.40 AM

RUN THE SCRIPT

Screen Shot 2015-02-06 at 8.14.54 AM

AFTER

Screen Shot 2015-02-06 at 8.16.00 AM

 

The code is below, as well as located on my github.

# Dropzone Action Info
# Name: Cleanup
# Description: Cleanup a folder based on extension
# Handles: Files
# Events: Dragged
# Creator: Michael Evans
# URL: https://sites.udel.edu/mdevans
# Version: 1.0
# RunsSandboxed: No
# UniqueID: 55
# MinDropzoneVersion: 3.0

def dragged

require 'fileutils'

ext = $dz.inputbox("Extension", "Please enter the file extension", "Extension")
ext = ext.chomp
ext = ext.tr('.','')
count = 0
$items.each do |dirName|

if File.directory?(dirName)

newDir = ext+"s"

ext = "*."+ext

Dir.mkdir(dirName+"/"+newDir) unless File.exists?(dirName+"/"+newDir)
$dz.begin("moving files")
searchPath = dirName + '/**/'+ ext
Dir[searchPath].reject{ |f| f[dirName+'/'+newDir]}.each do |filename|
if File.file?(filename)

if (FileUtils.mv(filename, dirName+"/"+newDir) unless File.exists?(dirName+"/"+newDir+"/"+File.basename(filename)))
count+=1
end
end
end

end

end
$dz.finish("moved #{count} files")
end