Skip to content

Instantly share code, notes, and snippets.

View portante's full-sized avatar

Peter Portante portante

View GitHub Profile
@portante
portante / example_journal_reader.c
Last active March 23, 2021 13:30
Simple C program to demonstrate how to properly "tail" journald, what journalctl -f does, while detecting journal file rotation to avoid keeping deleted files open (basically call sd_journal_get_fd() immediately after sd_journal_open()).
// cc -g2 example_journal_reader.c -lsystemd
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <sys/poll.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <systemd/sd-journal.h>
@portante
portante / example-operations-template.txt
Last active January 2, 2018 15:02
Aggregated Logging ".operations.*" template for N primary shards
sh-4.2$ cat operations.template.json
{
"order" : 20,
"template" : ".operations.*",
"settings" : {
"index" : {
"number_of_shards" : "3"
}
}
}
@portante
portante / get_pod_metadata.rb
Last active December 8, 2017 22:15
Example re-work of get_pod_metadata.
def get_pod_metadata(key, namespace_name, pod_name, record_create_time)
metadata = nil
ids = @id_cache[key]
if !ids.nil?
# FAST PATH
# Cache hit, fetch metadata from the cache
metadata = @cache.fetch(ids[:pod_id]) do
@stats.bump(:pod_cache_miss)
m = fetch_pod_metadata(namespace_name, pod_name)
(m.nil? || m.empty?) ? {'pod_id'=>ids[:pod_id]} : m
@portante
portante / format-pods.py
Last active August 14, 2019 07:31
Format output from "oc get pods -o json" or "oc adm manage-node --list-pods --output=json" into a list with container, pod, namespace, cpu, memory, host
#!/usr/bin/env python
import sys
import os
import json
if sys.argv[1] == '-':
pods = json.load(sys.stdin)
else:
with open(sys.argv[1], "r") as fp:
@portante
portante / allocate-shard.sh
Last active March 7, 2018 21:32
A simple script to "allocate" a primary shard on an particular OpenShift pod ES cluster member.
#!/bin/bash
ES_URL='https://localhost:9200'
curl_post='curl -s -X POST --cacert /etc/elasticsearch/secret/admin-ca --cert /etc/elasticsearch/secret/admin-cert --key /etc/elasticsearch/secret/admin-key'
date
$curl_post $ES_URL/_cluster/reroute?pretty=true -d "{ \"commands\" : [ { \"allocate\" : { \"index\" : \"$2\", \"shard\" : 0, \"node\" : \"$1\", \"allow_primary\": \"true\" } } ] }"
@portante
portante / change-replica-count-1.sh
Last active April 5, 2018 11:03
A simple script to set the replica count for all indices to 1.
#!/bin/bash
ES_URL='https://localhost:9200'
curl_put='curl -s -X PUT --cacert /etc/elasticsearch/secret/admin-ca --cert /etc/elasticsearch/secret/admin-cert --key /etc/elasticsearch/secret/admin-key'
$curl_put $ES_URL/${1}/_settings?pretty -d '{ "index" : { "auto_expand_replicas" : "false" } }'
$curl_put $ES_URL/${1}/_settings?pretty -d '{ "index" : { "number_of_replicas" : 1 } }'
@portante
portante / check-local-controller.sh
Last active August 10, 2017 01:05
How to tell which host is the active controller in OpenShift
#!/bin/bash
# The actively running controller returns 200 for /controllers on port 8444.
oc get --raw /controllers --server https://localhost:8444
# You'll get 201 accepted if the controller is waiting
@portante
portante / check_fluentd_access.sh
Created August 2, 2017 01:11
Check that a fluentd pod can access Elasticsearch in an OpenShift 3.4 and later deployment
#!/bin/bash
_BASE=/etc/fluent/keys
_CA=$_BASE/ca
_CERT=$_BASE/cert
_KEY=$_BASE/key
ls -l $_CA $_CERT $_KEY
ES_URL='https://logging-es:9200'
curl_get="curl -X GET --cacert $_CA --cert $_CERT --key $_KEY"
@portante
portante / example-oc-exec-bulk-stats.log
Created August 1, 2017 21:42
Example oc exec to fetch bulk stats from ES in OpenShift (3.4 and later)
root@ip-172-31-49-18: ~ # export espod=logging-es-data-master-siovwe17-7-hxpp9
root@ip-172-31-49-18: ~ # oc exec $espod -- curl -s -k --cert /etc/elasticsearch/secret/admin-cert --key /etc/elasticsearch/secret/admin-key https://localhost:9200/_cat/thread_pool?v\&h=host,bulk.rejected,bulk.queue,bulk.queueSize,bulk.largest,bulk.completed
host bulk.rejected bulk.queue bulk.queueSize bulk.largest bulk.completed
172.20.9.25 0 0 404 0 0
172.20.1.25 0 0 404 4 35
172.20.2.30 0 0 404 4 18
@portante
portante / watch-count.sh
Last active July 11, 2017 02:29
A simple script to emit a timestamped number of the count of documents currently in ES
#!/bin/bash
ES_URL='https://localhost:9200'
curl_get='curl -s -X GET --cacert /etc/elasticsearch/secret/admin-ca --cert /etc/elasticsearch/secret/admin-cert --key /etc/elasticsearch/secret/admin-key'
# Full Date Stamp Epoch Timestamp Count
# 2017-07-11T02:25:50 1499739950 02:25:50 36208782
for i in {1..30}; do
echo "$(date +%Y-%m-%dT%H:%M:%S) $($curl_get $ES_URL/_cat/count)"