Skip to content

Instantly share code, notes, and snippets.

@mitio
mitio / test-direct-http-request-to-passenger
Created March 10, 2014 21:33
A quick script to call a Phusion Passenger process directly to assist in debugging server issues.
#!/usr/bin/env ruby
status = `passenger-status -v --show=xml`
pid_to_show = ARGV.first
processes = {}
def extract(tag_name, xml)
if xml =~ /<#{tag_name}>(.*?)<\/#{tag_name}>/s
$1.to_s.strip
@mitio
mitio / check_for_late_commits.rb
Last active August 29, 2015 13:56
Quick scripts to help me grade hw 05 for the "Programming Ruby" course -- http://2013.fmi.ruby.bg/tasks/5
require 'time'
task_ends_at = '2014-01-22 17:30:00 +200'
late_commits_unix_timestamp = Time.parse(task_ends_at).utc.to_i
def commit_timestamp_of(commit_line)
commit_line.split(/\s+/).first.to_i
end
@mitio
mitio / execute_with_env
Created January 23, 2014 11:03
Some tools do not load a user's environment ($HOME, $PATH, etc.) when they run commands as that user. Sometimes, though, we need that environment. This script attempts to load it properly, before executing the command passed to it as arguments.
#!/bin/sh
RUNNING_AS=`whoami`
export HOME=`getent passwd $RUNNING_AS | cut -d: -f6`
source ~/.bashrc
exec "$@"
@mitio
mitio / apache-stats
Last active June 1, 2021 09:27
A few tiny shell scripts for quick diagnostics of possible web server problems
#!/bin/sh
grep -E '^Listen ' /etc/apache2/ports.conf | sed 's/:/./' | sed 's/Listen //' | \
ruby -e 'conns = []; while conn = gets; conns << conn.strip; end; system "netstat -an|grep tcp|grep -v LISTEN|grep -E \"\\b#{conns.join "\\b|\\b"}\\b\"";' | awk '{ print $5 }' | ruby -e 'conns = Hash.new(0); while conn = gets; parts = conn.strip.split(":"); ip = parts[0]; port = parts[-1]; conns[ip] += 1; end; conns.sort_by { |ip, count| -count }.each { |ip, count| puts "IP: #{ip.ljust(25)} connections: #{count.to_s.ljust(8)}" } ;nil'
@mitio
mitio / fix_cp1251_encoded_as_utf8.rb
Created December 20, 2013 17:43
Fix broken cyrillic, originally written in windows-1251 (cp1251), but wrongly interpreted as valid UTF-8 and encoded as such.
# Windows-1251 encoded as UTF-8
def decode_broken_cyrillic(s)
offset = 848
utf8_chars = s.chars.map do |char|
if char.ord > 127
[char.ord + offset].pack('U')
else
char
end
@mitio
mitio / block_objects_speed.rb
Last active December 27, 2015 23:19
A small benchmark testing the toll of converting a block to a Proc object.
# With Ruby 2.1.0-preview1 this benchmark outputs:
#
# user system total real
# calling_with_param 1.450000 0.150000 1.600000 ( 1.874802)
# calling_with_proc_new 0.920000 0.060000 0.980000 ( 1.004593)
# calling_with_yield 0.220000 0.000000 0.220000 ( 0.233624)
# returning_with_param 1.300000 0.080000 1.380000 ( 1.414610)
# returning_with_proc_new 0.830000 0.040000 0.870000 ( 0.880629)
require 'benchmark'
FFFFFFFFFFFF...FF....F
Failures:
1) TodoList filters tasks by status
Failure/Error: todo_list.filter(Criteria.status :done).map(&:description).should =~ [
NoMethodError:
undefined method `status' for nil:NilClass
# ./hw02_adelina.rb:82:in `status'
# /Users/dimitardimitrov/Projects/web/ruby-homework/tasks/02/spec.rb:19:in `block (2 levels) in <top (required)>'
config defaultToCurrentScreen true
config nudgePercentOf screenSize
config resizePercentOf screenSize
# Abstract positions
alias full move screenOriginX;screenOriginY screenSizeX;screenSizeY
alias lefthalf move screenOriginX;screenOriginY screenSizeX/2;screenSizeY
alias righthalf move screenOriginX+screenSizeX/2;screenOriginY screenSizeX/2;screenSizeY
alias topleft corner top-left resize:screenSizeX/2;screenSizeY/2
alias topright corner top-right resize:screenSizeX/2;screenSizeY/2
@mitio
mitio / readme.md
Last active August 15, 2022 00:57
A small script to run a single command on multiple servers.

Run a command on multiple servers

A small script to allow running the same command on multiple servers. The commands are ran in series so that if you see an error somewhere, you can quickly bail out with Ctrl + C and avoid further damage to your servers.

Usage

Run as:

run-on dbserver.mysite.com frontend.mysite.com 'date; df -h /tmp'
def homogenize(items)
arr_with_integers = []
arr_with_rational = []
arr_with_float = []
arr_with_strings = []
arr_with_symbols = []
arr_with_hash = []
arr_with_array = []
arr_nil_class = []
arr_with_classes = []