summaryrefslogtreecommitdiffstats
path: root/collectors/ioping.plugin/ioping.plugin.in
blob: 171e384dbf3b37442e41a2cab3d5dace71d57dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later

# netdata
# real-time performance and health monitoring, done right!
# (C) 2017 Costa Tsaousis <costa@tsaousis.gr>
# GPL v3+
#
# This plugin requires a latest version of ioping.
# You can compile it from source, by running me with option: install

export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin:@sbindir_POST@"
export LC_ALL=C

usage="$(basename "$0") [install] [-h] [-e]

where:
    install      install ioping binary
    -e, --env    path to environment file (defaults to '/etc/netdata/.environment'
    -h           show this help text"

INSTALL=0
ENVIRONMENT_FILE="/etc/netdata/.environment"

while :; do
    case "$1" in
    -h | --help)
        echo "$usage" >&2
        exit 1
        ;;
    install)
        INSTALL=1
        shift
        ;;
    -e | --env)
        ENVIRONMENT_FILE="$2"
        shift 2
        ;;
    -*)
        echo "$usage" >&2
        exit 1
        ;;
    *) break ;;
    esac
done

if [ "$INSTALL" == "1" ]
    then
    [ "${UID}" != 0 ] && echo >&2 "Please run me as root. This will install a single binary file: /usr/libexec/netdata/plugins.d/ioping." && exit 1

    source "${ENVIRONMENT_FILE}" || exit 1

    run() {
        printf >&2 " > "
        printf >&2 "%q " "${@}"
        printf >&2 "\n"
        "${@}" || exit 1
    }

    download() {
        local git="$(which git 2>/dev/null || command -v git 2>/dev/null)"
        [ ! -z "${git}" ] && run git clone "${1}" "${2}" && return 0

        echo >&2 "Cannot find 'git' in this system." && exit 1
    }

    tmp=$(mktemp -d /tmp/netdata-ioping-XXXXXX)
    [ ! -d "${NETDATA_PREFIX}/usr/libexec/netdata" ] && run mkdir -p "${NETDATA_PREFIX}/usr/libexec/netdata"

    run cd "${tmp}"

    if [ -d ioping-netdata ]
        then
        run rm -rf ioping-netdata || exit 1
    fi

    download 'https://github.com/netdata/ioping.git' 'ioping-netdata'
    [ $? -ne 0 ] && exit 1
    run cd ioping-netdata || exit 1

    INSTALL_PATH="${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/ioping"

    run make clean
    run make
    run mv ioping "${INSTALL_PATH}"
    run chown root:"${NETDATA_GROUP}" "${INSTALL_PATH}"
    run chmod 4750 "${INSTALL_PATH}"
    echo >&2
    echo >&2 "All done, you have a compatible ioping now at ${INSTALL_PATH}."
    echo >&2

    exit 0
fi

# -----------------------------------------------------------------------------
# logging

PROGRAM_NAME="$(basename "${0}")"

# these should be the same with syslog() priorities
NDLP_EMERG=0   # system is unusable
NDLP_ALERT=1   # action must be taken immediately
NDLP_CRIT=2    # critical conditions
NDLP_ERR=3     # error conditions
NDLP_WARN=4    # warning conditions
NDLP_NOTICE=5  # normal but significant condition
NDLP_INFO=6    # informational
NDLP_DEBUG=7   # debug-level messages

# the max (numerically) log level we will log
LOG_LEVEL=$NDLP_INFO

set_log_min_priority() {
  case "${NETDATA_LOG_LEVEL,,}" in
    "emerg" | "emergency")
      LOG_LEVEL=$NDLP_EMERG
      ;;

    "alert")
      LOG_LEVEL=$NDLP_ALERT
      ;;

    "crit" | "critical")
      LOG_LEVEL=$NDLP_CRIT
      ;;

    "err" | "error")
      LOG_LEVEL=$NDLP_ERR
      ;;

    "warn" | "warning")
      LOG_LEVEL=$NDLP_WARN
      ;;

    "notice")
      LOG_LEVEL=$NDLP_NOTICE
      ;;

    "info")
      LOG_LEVEL=$NDLP_INFO
      ;;

    "debug")
      LOG_LEVEL=$NDLP_DEBUG
      ;;
  esac
}

set_log_min_priority

log() {
  local level="${1}"
  shift 1

  [[ -n "$level" && -n "$LOG_LEVEL" && "$level" -gt "$LOG_LEVEL" ]] && return

  systemd-cat-native --log-as-netdata --newline="--NEWLINE--" <<EOFLOG
INVOCATION_ID=${NETDATA_INVOCATION_ID}
SYSLOG_IDENTIFIER=${PROGRAM_NAME}
PRIORITY=${level}
THREAD_TAG=ioping.plugin
ND_LOG_SOURCE=collector
MESSAGE=${MODULE_NAME}: ${*//\\n/--NEWLINE--}

EOFLOG
  # AN EMPTY LINE IS NEEDED ABOVE
}

info() {
  log "$NDLP_INFO" "${@}"
}

warning() {
  log "$NDLP_WARN" "${@}"
}

error() {
  log "$NDLP_ERR" "${@}"
}

disable() {
  log "${@}"
	echo "DISABLE"
  exit 1
}

fatal() {
  disable "$NDLP_ALERT" "${@}"
}

debug() {
  log "$NDLP_DEBUG" "${@}"
}

# -----------------------------------------------------------------------------

# store in ${plugin} the name we run under
# this allows us to copy/link ioping.plugin under a different name
# to have multiple ioping plugins running with different settings
plugin="${PROGRAM_NAME/.plugin/}"


# -----------------------------------------------------------------------------

# the frequency to send info to netdata
# passed by netdata as the first parameter
update_every="${1-1}"

# the netdata configuration directory
# passed by netdata as an environment variable
[ -z "${NETDATA_USER_CONFIG_DIR}"  ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
[ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"

# the netdata directory for internal binaries
[ -z "${NETDATA_PLUGINS_DIR}"      ] && NETDATA_PLUGINS_DIR="@pluginsdir_POST@"

# -----------------------------------------------------------------------------
# configuration options
# can be overwritten at /etc/netdata/ioping.conf

# the ioping binary to use
# we need one that can output netdata friendly info (supporting: -N)
# if you have multiple versions, put here the full filename of the right one
ioping="${NETDATA_PLUGINS_DIR}/ioping"

# the destination to ioping
destination=""

# the request size in bytes to ping the disk
request_size="4k"

# ioping options
ioping_opts="-T 1000000"

# -----------------------------------------------------------------------------
# load the configuration files

for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/${plugin}.conf" "${NETDATA_USER_CONFIG_DIR}/${plugin}.conf"; do
  if [ -f "${CONFIG}" ]; then
    debug "Loading config file '${CONFIG}'..."
    source "${CONFIG}"
    [ $? -ne 0 ] && warn "Failed to load config file '${CONFIG}'."
  elif [[ $CONFIG =~ ^$NETDATA_USER_CONFIG_DIR ]]; then
    debug "Cannot find file '${CONFIG}'."
  fi
done

if [ -z "${destination}" ]
then
	disable $NDLP_DEBUG "destination is not configured - nothing to do."
fi

if [ ! -f "${ioping}" ]
then
	disable $NDLP_ERR "ioping command is not found. Please set its full path in '${NETDATA_USER_CONFIG_DIR}/${plugin}.conf'"
fi

if [ ! -x "${ioping}" ]
then
	disable $NDLP_ERR "ioping command '${ioping}' is not executable - cannot proceed."
fi

# the ioping options we will use
options=( -N -i ${update_every} -s ${request_size} ${ioping_opts} ${destination} )

# execute ioping
debug "starting ioping: ${ioping} ${options[*]}"

exec "${ioping}" "${options[@]}"

# if we cannot execute ioping, stop
error "command '${ioping} ${options[*]}' failed to be executed (returned code $?)."