Skip to content

Instantly share code, notes, and snippets.

View nirebu's full-sized avatar

Nicolò Rebughini nirebu

View GitHub Profile
@nirebu
nirebu / MongoBatchBoilerplate.js
Last active July 19, 2018 13:45
[MongoDB batch boilerplate] Basic boilerplate code for mongo shell batch scripts #mongodb
print(new Date() + ' Script launched');
const dbName = "";
const inputCollection = "";
const outputCollection = "";
let conn = new Mongo({w:1, journal:false, safe: true});
let dbConn = conn.getDB(dbName);
let collIn = dbConn[inputCollection];
@nirebu
nirebu / MongodbAggregateLongQueries.js
Last active January 28, 2019 09:45
[Mongo DB long queries aggregation] Aggregate for queries running on shards to detect if there are too many or too long queries slowing the system down
use admin
var threshold = 500;
db.aggregate([
{ $currentOp : { allUsers: true } },
{ $match: { "secs_running" : { $gt: threshold } , "ns" : { $not : /oplog/ } } },
{ $group : {
_id: '$shard',
"n_query": { $sum: 1 },
@nirebu
nirebu / ansible-gather-ipv4.sh
Last active February 5, 2019 13:29
Collect and transform IPv4 data via Ansible and jq
#!/bin/bash
ansible -i 'localhost,' all -m setup | sed '1c {' | jq '.ansible_facts.ansible_all_ipv4_addresses[] | { address: . }' --compact-output
@nirebu
nirebu / ensure-package-excluded.yml
Last active April 5, 2019 13:36
Ensure open-vm-tools is excluded from /etc/yum.conf after installing the latest version from source #ansible #open-vm-tools #yum
- name: OPEN VM TOOLS | Checking how many matches are there for the lines exclude and open-vm-tools
shell: (grep -oP "(^exclude=|open-vm-tools)" /etc/yum.conf | wc -l) || true
register: test_grep
# 0 matches: will add the exclude line with open-vm-tools
# 1 match: only exclude present, unless it's a badly formatted /etc/yum.conf or the second pattern is in some comment
# 2 matches: both patterns present, will do nothing
- debug:
msg: "Matches: {{ test_grep.stdout }}"
@nirebu
nirebu / spec_helper.rb
Created July 15, 2022 13:09
Enable conditional factory bot monitoring in RSpec
if ENV['MONITOR_FACTORIES']
factory_results = {
time_spent_in_factories: 0,
payloads: [],
}
config.before do
ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |_name, start, finish, _id, payload|
took = finish - start
factory_results[:payloads] << payload