Skip to content

Instantly share code, notes, and snippets.

View dberzano's full-sized avatar

Dario Berzano dberzano

View GitHub Profile
@dberzano
dberzano / getoutipv4.cpp
Last active January 3, 2016 20:39
Returns the IPv4 address used for outbound connections by means of a "dummy" UDP socket. Tested on Linux and OS X (it should be POSIX compliant).
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
int getoutipv4(std::string *outip) {
if (!outip)
@dberzano
dberzano / condor_user_quotas
Last active August 29, 2015 13:58
HTCondor: per-user "quotas" using match making
#
# "Quotas" per user
#
# Our requirements contain an expression that changes at every accepted job. We
# cannot therefore optimize matchmaking by caching the results for a specific
# "requirements" string, but we will need to evaluate it per job.
NEGOTIATOR_MATCHLIST_CACHING = False
# The following two variables are set to enforce these "quotas" on setups with
@dberzano
dberzano / patch-ucvm-mtu.sh
Created July 21, 2014 16:31
Patch MTU on KVM uCernVM image
#!/bin/bash
cd $(dirname "$0")
ucvm=$PWD/ucernvm-prod.1.18-2.cernvm.x86_64.hdd
[ ! -e ${ucvm}.orig ] && cp $ucvm ${ucvm}.orig
t=$(mktemp -d /tmp/cvm-initrd-XXXXX)
cd $t
echo starting guestfish
eval "`guestfish --listen`" || exit 1
echo mounting fs
guestfish --remote add $ucvm : run : mount /dev/sda1 / || exit 1
@dberzano
dberzano / condor_job_memory
Created October 23, 2014 08:48
HTCondor: define memory policies
## Pretend that all jobs have very low memory requirements: useful
## when running pilot jobs (e.g. AliEn)
JOB_DEFAULT_REQUESTMEMORY = 42
@dberzano
dberzano / ShowRootMaps.C
Created January 27, 2015 21:24
Show current rootmaps in ROOT
void ShowRootMaps() {
TObjArray *oa = gInterpreter->GetRootMapFiles();
TIter i(oa);
TObject *o;
TNamed *n;
TString s;
while ((o = i.Next()) != NULL) {
n = dynamic_cast<TNamed *>(o);
Printf("%-40s --> %s", n->GetName(), n->GetTitle());
}
@dberzano
dberzano / CMakeLists.txt
Created January 28, 2015 11:11
CMake: how to use optional arguments in macros and how to escape/unescape args to a shell
cmake_minimum_required(VERSION 2.8)
project(testoptargsandescape)
macro(testmacro testarg1 testarg2)
message(STATUS "testarg1 = \"${testarg1}\"")
message(STATUS "testarg2 = \"${testarg2}\"")
message(STATUS "Optional arg = \"${ARGV2}\"")
@dberzano
dberzano / cvm-context-api.py
Created January 30, 2015 16:39
Get CernVM Online context from API
#!/usr/bin/env python
# Usage:
# cvm-context-api.py [--user-data] <context_id> [<context_key>]
import sys
import requests
import base64
import hashlib
from Crypto.Cipher import AES
@dberzano
dberzano / cpueff.py
Created November 11, 2015 13:43
CPU efficiency on Linux with Python
#!/usr/bin/env python
from time import sleep
ncpus = sum([ 1 for x in open("/proc/cpuinfo").read().split("\n") if "bogomips" in x ])
def getup():
tot,idle = open("/proc/uptime").read().split(" ")
return float(tot)*ncpus,float(idle)
@dberzano
dberzano / memdec.py
Last active March 7, 2017 08:14
Python decorators for class members
#!/usr/bin/env python
# Will print:
# init method
# decorator here
# called with a message
# 12345
# modern decorator here with decorator with a message
# yabba with another message
# 54321
#!/usr/bin/python
from __future__ import print_function
def disaster_function(a_default=[]):
# BEWARE: the next line, if a_default is not provided, modifies the default
# object, and **not a copy** of the default object! This is because you are
# modifying in place a "name" pointing to the default, by effectively changing
# the default value for all future function calls
a_default.append("a string")
print("disaster_function(): %s" % a_default)