Skip to content

Commit

Permalink
initial commit of data from old location
Browse files Browse the repository at this point in the history
  • Loading branch information
fredex42 committed Aug 18, 2022
0 parents commit a48fc40
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ruby:2.6-alpine

RUN gem install awesome_print elasticsearch optimist
COPY src/spacemon.rb /usr/local/bin/spacemon.rb
COPY src/docker_entrypoint.sh /docker_entrypoint.sh
RUN chmod a+x /docker_entrypoint.sh
ENTRYPOINT /docker_entrypoint.sh
41 changes: 41 additions & 0 deletions spacemon-deployment-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: spacemon
labels:
com.theguardian.app: Multimedia
com.theguardian.stack: spacemon
spec:
replicas: 1
revisionHistoryLimit: 5 #clean up replica sets older than this
selector:
matchLabels:
com.theguardian.app: Multimedia
com.theguardian.stack: spacemon
template:
metadata:
labels:
com.theguardian.app: Multimedia
com.theguardian.stack: spacemon
spec:
nodeSelector:
has-san: "true"
containers:
- image: published-image-here
name: spacemon
securityContext:
allowPrivilegeEscalation: false
env:
- name: ELASTICSEARCH_HOST
value: elasticsearch:9200
- name: SLEEP_TIME
value: "120"
- name: FILTER
value: "/srv"
#mount the volumes you want to scan here
#volumeMounts:
#configure the volumes that you want to scan as hostPath volumes here,
#then mount them above in volumeMounts
#volumes:

restartPolicy: Always
14 changes: 14 additions & 0 deletions src/docker_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

if [ "$SLEEP_TIME" == "" ]; then
SLEEP_TIME=60
fi

while `/bin/true`; do
ruby /usr/local/bin/spacemon.rb --elasticsearch $ELASTICSEARCH_HOST --filter $FILTER
if [ "$?" != "0" ]; then
echo Command failed, exiting
exit $?
fi
sleep $SLEEP_TIME
done
70 changes: 70 additions & 0 deletions src/spacemon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env ruby

require 'elasticsearch'
require 'logger'
require 'awesome_print'
require 'socket'
require 'optimist'

INDEXNAME = "diskspace"
TYPENAME = "diskspace"

#START MAIN
opts = Optimist::options do
opt :elasticsearch, "Elasticsearch hosts", :type=>:string
opt :filter, "Only include these filesystems. Specify as a regex.", :type=>:string
end

lines = `df -P`.split(/\n/)

have_header = false
header = []

content = []

lines.each {|line|
if not have_header
header = line.split(/\s+/)
have_header = true
next
end

parts = /^(.*\S)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%\s+(.*)$/.match(line)
if parts
data = Hash[header.zip(parts.captures)]
data.each {|k,v|
begin
data[k] = Integer(v)
rescue StandardError
end

}
data['Hostname'] = Socket.gethostname
data['Timestamp'] = DateTime.now
content << data
end

}

if opts.filter
ap opts.filter
matcher = Regexp.compile(opts[:filter])
filtered_content = content.filter { |entry|
matcher.match(entry["Mounted"])
}
else
filtered_content = content
end

ap filtered_content

if opts.elasticsearch
es = Elasticsearch::Client.new(:hosts=>opts.elasticsearch)
result=es.cluster.health

ops = []
filtered_content.each do |entry|
ops << {index: {_index: INDEXNAME, _type: TYPENAME, data: entry}}
end
es.bulk(body: ops)
end

0 comments on commit a48fc40

Please sign in to comment.