forked from 42ity/fty-nut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfty-dmf
More file actions
executable file
·126 lines (109 loc) · 3.35 KB
/
fty-dmf
File metadata and controls
executable file
·126 lines (109 loc) · 3.35 KB
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
#!/bin/bash
#
# Copyright (C) 2016 - 2020 Eaton
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#! \file fty-dmf
# \brief Helper script handling DMF files (part of Extreme support)
# \author Michal Vyskocil <MichalVyskocil@Eaton.com>
# \details Helper script for handling DMF files, it can copy and enable things
#
if [ "$(id -u)" != "0" ] ; then
exec sudo "$0" "$@"
fi
set -e
export PATH=/bin:/usr/bin
export LC_ALL=C
#hack for Makefile.am and make distcheck
export PROG=$(readlink -f "${0}")
die() {
echo "${@}" >&2
exit 1
}
source /etc/default/bios-db-rw
TMPDIR=${TMPDIR:-/tmp}
NUTCONFIGDIR=/etc/nut
[ -d "$NUTCONFIGDIR" ] || NUTCONFIGDIR=/etc/ups
[ -d "$NUTCONFIGDIR" ] || die "NUT configuration directory not found"
NUTCONFIG="$NUTCONFIGDIR/ups.conf"
DMFSNMPD=/usr/share/nut/dmfsnmp.d/
DMFSNMP=/usr/share/nut/dmfsnmp/
[ -d "${DMFSNMPD}" ] || die "Can't find NUT DMF runtime dir at ${DMFSNMPD}"
if [[ -z "${BIOS_USER}" ]]; then
source /etc/default/_bios-script
fi
usage () {
echo "${PROG}"
echo "upload dmf1 [dmf2 ...] - upload dmf files to ${DMFSNMPD}"
echo "enable asset1 [asset2 ...] - enable dmf for particular asset"
echo "list - list all assets with enabled dmf"
}
do_mysql() {
mysql -N -B -u "${DB_USER}" -p"${DB_PASSWD}" box_utf8 -e "$@"
}
get_asset_ids () {
condition=`echo "${@}" | sed -E "s/([^[:space:]]+)/name=\'\1\'/g" | sed 's/ / OR /g'`
do_mysql "SELECT id FROM v_bios_asset_element WHERE (${condition})"
}
do_upload () {
cp "${@}" -t ${DMFSNMP}
for dmf in "${@}"; do
DMFNAME=`basename $dmf`
ln -s ${DMFSNMP}${DMFNAME} -t ${DMFSNMPD}
done
}
do_enable () {
for id in `get_asset_ids "${@}"`; do
do_mysql "INSERT INTO v_bios_asset_ext_attributes (keytag, value, id_asset_element, read_only) \
VALUES ('upsconf_enable_dmf', 'true', $id, 0) \
ON DUPLICATE KEY UPDATE value='true';
"
done
fty-asset-cli republish "${@}"
}
do_disable () {
for id in `get_asset_ids "${@}"`; do
do_mysql "DELETE FROM v_bios_asset_ext_attributes WHERE (keytag = 'upsconf_enable_dmf' AND id_asset_element=$id)"
done
fty-asset-cli republish "${@}"
}
do_list () {
do_mysql 'SELECT t1.name FROM v_bios_asset_element t1 LEFT JOIN v_bios_asset_ext_attributes t2 ON t1.id = t2.id_asset_element WHERE t2.keytag="upsconf_enable_dmf" AND t2.value="true";'
}
COMMAND=${1}
if [[ -z "${COMMAND}" ]]; then
COMMAND="--help"
else
shift 1
fi
case "${COMMAND}" in
"upload")
do_upload "${@}"
;;
"enable")
do_enable "${@}"
;;
"disable")
do_disable "${@}"
;;
"list")
do_list "${@}"
;;
*)
usage
exit 1
;;
esac