aboutsummaryrefslogtreecommitdiff
path: root/ttar
diff options
context:
space:
mode:
authorideaship <ideaship@users.noreply.github.com>2017-07-07 07:20:18 +0200
committerBen Kochie <superq@gmail.com>2017-07-07 07:20:18 +0200
commit8d902762839282f0c2bf3b77fe33cf5fbfa1d8df (patch)
treec36f6fab0729738d83f7b357b1547ad1777815c7 /ttar
parentbba075710d52296961b9f2ab8f72c9b19cbd2117 (diff)
downloadprometheus_node_collector-8d902762839282f0c2bf3b77fe33cf5fbfa1d8df.tar.bz2
prometheus_node_collector-8d902762839282f0c2bf3b77fe33cf5fbfa1d8df.tar.xz
prometheus_node_collector-8d902762839282f0c2bf3b77fe33cf5fbfa1d8df.zip
Add bcache collector (#597)
* Add bcache collector for Linux This collector gathers metrics related to the Linux block cache (bcache) from sysfs. * Removed commented out code * Use project comment style * Add _sectors to metric name to indicate unit * Really use project comment style * Rename bcache.go to bcache_linux.go * Keep collector namespace clean Rename: - metric -> bcacheMetric - periodStatsToMetrics -> bcachePeriodStatsToMetric * Shorten slice initialization * Change label names to backing_device, cache_device * Remove five minute metrics (keep only total) * Include units in additional metric names * Enable bcache collector by default * Provide metrics in seconds, not nanoseconds * remove metrics with label "all" * Add fixtures, update end-to-end for bcache collector * Move fixtures/sys into tar.gz This changeset moves the collector/fixtures/sys directory into collector/fixtures/sys.tar.gz and tweaks the Makefile to unpack the tarball before tests are run. The reason for this change is that Windows does not allow colons in a path (colons are present in some of the bcache fixture files), nor can it (out of the box) deal with pathnames longer than 260 characters (which we would be increasingly likely to hit if we tried to replace colons with longer codes that are guaranteed not the turn up in regular file names). * Add ttar: plain text archive, replacement for tar This changeset adds ttar, a plain text replacement for tar, and uses it for the sysfs fixture archive. The syntax is loosely based on tar(1). Using a plain text archive makes it possible to review changes without downloading and extracting the archive. Also, when working on the repo, git diff and git log become useful again, allowing a committer to verify and track changes over time. The code is written in bash, because bash is available out of the box on all major flavors of Linux and on macOS. The feature set used is restricted to bash version 3.2 because that is what Apple is still shipping. The programm also works on Windows if bash is installed. Obviously, it does not solve the Windows limitations (path length limited to 260 characters, no symbolic links) that prompted the move to an archive format in the first place.
Diffstat (limited to 'ttar')
-rwxr-xr-xttar266
1 files changed, 266 insertions, 0 deletions
diff --git a/ttar b/ttar
new file mode 100755
index 0000000..c3472c1
--- /dev/null
+++ b/ttar
@@ -0,0 +1,266 @@
1#!/usr/bin/env bash
2# Purpose: plain text tar format
3# Limitations: - only suitable for text files, directories, and symlinks
4# - stores only filename, content, and mode
5# - not designed for untrusted input
6
7# Note: must work with bash version 3.2 (macOS)
8
9set -o errexit -o nounset
10
11# Sanitize environment (for instance, standard sorting of glob matches)
12export LC_ALL=C
13
14path=""
15CMD=""
16ARG_STRING="$@"
17
18function usage {
19 bname=$(basename "$0")
20 cat << USAGE
21Usage: $bname [-C <DIR>] -c -f <ARCHIVE> <FILE...> (create archive)
22 $bname -t -f <ARCHIVE> (list archive contents)
23 $bname [-C <DIR>] -x -f <ARCHIVE> (extract archive)
24
25Options:
26 -C <DIR> (change directory)
27
28Example: Change to sysfs directory, create ttar file from fixtures directory
29 $bname -C sysfs -c -f sysfs/fixtures.ttar fixtures/
30USAGE
31exit "$1"
32}
33
34function vecho {
35 if [ "${VERBOSE:-}" == "yes" ]; then
36 echo >&7 "$@"
37 fi
38}
39
40function set_cmd {
41 if [ -n "$CMD" ]; then
42 echo "ERROR: more than one command given"
43 echo
44 usage 2
45 fi
46 CMD=$1
47}
48
49while getopts :cf:htxvC: opt; do
50 case $opt in
51 c)
52 set_cmd "create"
53 ;;
54 f)
55 ARCHIVE=$OPTARG
56 ;;
57 h)
58 usage 0
59 ;;
60 t)
61 set_cmd "list"
62 ;;
63 x)
64 set_cmd "extract"
65 ;;
66 v)
67 VERBOSE=yes
68 exec 7>&1
69 ;;
70 C)
71 CDIR=$OPTARG
72 ;;
73 *)
74 echo >&2 "ERROR: invalid option -$OPTARG"
75 echo
76 usage 1
77 ;;
78 esac
79done
80
81# Remove processed options from arguments
82shift $(( OPTIND - 1 ));
83
84if [ "${CMD:-}" == "" ]; then
85 echo >&2 "ERROR: no command given"
86 echo
87 usage 1
88elif [ "${ARCHIVE:-}" == "" ]; then
89 echo >&2 "ERROR: no archive name given"
90 echo
91 usage 1
92fi
93
94function list {
95 local path=""
96 local size=0
97 local line_no=0
98 local ttar_file=$1
99 if [ -n "${2:-}" ]; then
100 echo >&2 "ERROR: too many arguments."
101 echo
102 usage 1
103 fi
104 if [ ! -e "$ttar_file" ]; then
105 echo >&2 "ERROR: file not found ($ttar_file)"
106 echo
107 usage 1
108 fi
109 while read -r line; do
110 line_no=$(( line_no + 1 ))
111 if [ $size -gt 0 ]; then
112 size=$(( size - 1 ))
113 continue
114 fi
115 if [[ $line =~ ^Path:\ (.*)$ ]]; then
116 path=${BASH_REMATCH[1]}
117 elif [[ $line =~ ^Lines:\ (.*)$ ]]; then
118 size=${BASH_REMATCH[1]}
119 echo "$path"
120 elif [[ $line =~ ^Directory:\ (.*)$ ]]; then
121 path=${BASH_REMATCH[1]}
122 echo "$path/"
123 elif [[ $line =~ ^SymlinkTo:\ (.*)$ ]]; then
124 echo "$path -> ${BASH_REMATCH[1]}"
125 fi
126 done < "$ttar_file"
127}
128
129function extract {
130 local path=""
131 local size=0
132 local line_no=0
133 local ttar_file=$1
134 if [ -n "${2:-}" ]; then
135 echo >&2 "ERROR: too many arguments."
136 echo
137 usage 1
138 fi
139 if [ ! -e "$ttar_file" ]; then
140 echo >&2 "ERROR: file not found ($ttar_file)"
141 echo
142 usage 1
143 fi
144 while IFS= read -r line; do
145 line_no=$(( line_no + 1 ))
146 if [ "$size" -gt 0 ]; then
147 echo "$line" >> "$path"
148 size=$(( size - 1 ))
149 continue
150 fi
151 if [[ $line =~ ^Path:\ (.*)$ ]]; then
152 path=${BASH_REMATCH[1]}
153 if [ -e "$path" ] || [ -L "$path" ]; then
154 rm "$path"
155 fi
156 elif [[ $line =~ ^Lines:\ (.*)$ ]]; then
157 size=${BASH_REMATCH[1]}
158 # Create file even if it is zero-length.
159 touch "$path"
160 vecho " $path"
161 elif [[ $line =~ ^Mode:\ (.*)$ ]]; then
162 mode=${BASH_REMATCH[1]}
163 chmod "$mode" "$path"
164 vecho "$mode"
165 elif [[ $line =~ ^Directory:\ (.*)$ ]]; then
166 path=${BASH_REMATCH[1]}
167 mkdir -p "$path"
168 vecho " $path/"
169 elif [[ $line =~ ^SymlinkTo:\ (.*)$ ]]; then
170 ln -s "${BASH_REMATCH[1]}" "$path"
171 vecho " $path -> ${BASH_REMATCH[1]}"
172 elif [[ $line =~ ^# ]]; then
173 # Ignore comments between files
174 continue
175 else
176 echo >&2 "ERROR: Unknown keyword on line $line_no: $line"
177 exit 1
178 fi
179 done < "$ttar_file"
180}
181
182function div {
183 echo "# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" \
184 "- - - - - -"
185}
186
187function get_mode {
188 local mfile=$1
189 if [ -z "${STAT_OPTION:-}" ]; then
190 if stat -c '%a' "$mfile" >/dev/null 2>&1; then
191 STAT_OPTION='-c'
192 STAT_FORMAT='%a'
193 else
194 STAT_OPTION='-f'
195 STAT_FORMAT='%A'
196 fi
197 fi
198 stat "${STAT_OPTION}" "${STAT_FORMAT}" "$mfile"
199}
200
201function _create {
202 shopt -s nullglob
203 local mode
204 while (( "$#" )); do
205 file=$1
206 if [ -L "$file" ]; then
207 echo "Path: $file"
208 symlinkTo=$(readlink "$file")
209 echo "SymlinkTo: $symlinkTo"
210 vecho " $file -> $symlinkTo"
211 div
212 elif [ -d "$file" ]; then
213 # Strip trailing slash (if there is one)
214 file=${file%/}
215 echo "Directory: $file"
216 mode=$(get_mode "$file")
217 echo "Mode: $mode"
218 vecho "$mode $file/"
219 div
220 # Find all files and dirs, including hidden/dot files
221 for x in "$file/"{*,.[^.]*}; do
222 _create "$x"
223 done
224 elif [ -f "$file" ]; then
225 echo "Path: $file"
226 lines=$(wc -l "$file"|awk '{print $1}')
227 echo "Lines: $lines"
228 cat "$file"
229 mode=$(get_mode "$file")
230 echo "Mode: $mode"
231 vecho "$mode $file"
232 div
233 else
234 echo >&2 "ERROR: file not found ($file in $(pwd))"
235 exit 2
236 fi
237 shift
238 done
239}
240
241function create {
242 ttar_file=$1
243 shift
244 if [ -z "${1:-}" ]; then
245 echo >&2 "ERROR: missing arguments."
246 echo
247 usage 1
248 fi
249 if [ -e "$ttar_file" ]; then
250 rm "$ttar_file"
251 fi
252 exec > "$ttar_file"
253 echo "# Archive created by ttar $ARG_STRING"
254 _create "$@"
255}
256
257if [ -n "${CDIR:-}" ]; then
258 if [[ "$ARCHIVE" != /* ]]; then
259 # Relative path: preserve the archive's location before changing
260 # directory
261 ARCHIVE="$(pwd)/$ARCHIVE"
262 fi
263 cd "$CDIR"
264fi
265
266"$CMD" "$ARCHIVE" "$@"