I would migrate from Nagios to Icinga2. I would try to migrate one my custom command that works in Nagios but i can't translate in Icinga.
I would create one my custom "object CheckCommand" to use in some hosts.
In nagios I have this (and still work if I execute on new Icinga2 server from shell)
./check_by_ssh -H myHostName -t 15 -l myLoginUsername -C "/opt/jboss/scripts/check_file_size.sh --maxwarn 80000000 --maxcrit 150000000 /opt/jboss/domain/servers/*/log/* /opt/jboss/domain/log/*" -E
Due to multiple level of parameter (parameter of check_by_ssh and parameter of check_file_size_ssh) I can not write the right "object CheckCommand" and "object Host" or "Apply Service".
Can someone help me?
Best reagrds
Try the following, adjust as necessary. Afaik the arguments can be specified as array and will be put together.
apply Service "ssh-check" {
import "generic-service"
check_command = "by_ssh"
vars.by_ssh_address = host.address
vars.by_ssh_command = "/opt/jboss/scripts/check_file_size.sh"
vars.by_ssh_logname = "myLoginUserName"
vars.by_ssh_arguments += [ "--maxwarn" ]
vars.by_ssh_arguments += [ "80000000" ]
vars.by_ssh_arguments += [ "--maxcrit" ]
vars.by_ssh_arguments += [ "150000000" ]
vars.by_ssh_arguments += [ "/opt/jboss/domain/servers/*/log/*" ]
vars.by_ssh_arguments += [ "/opt/jboss/domain/log/*" ]
}
Related
I've made a script that would ask the user for input and then evaluate the input from that file. I was wondering if I could do if else statements inside the cat string.
InfoCreateFile() {
touch $InfoFile
cat > "${InfoFile}" <<- 'EOF'
########################################
# System Information #
########################################
System IP=""
Domain=""
if [ "${panelinstall}" == "1" ]; then
Panel Subdomain=""
fi
if [ "${nodeinstall}" == "1" ]; then
Node Subdomain=""
fi
EOF
}
I know this is possible with arrays but I would like to do it without, arrays are a bit of a pain. Anyways, if there's no other solution.. could anyone give me an example of how I would do that using arrays?
In general, I'd just avoid cat here altogether; it's not giving you any value.
exec 3>"$InfoFile" # create and open output file
emit() { printf '%s\n' "$#" >&3; } # write each argument as a line in that file
emit '########################################'
emit '# System Information #'
emit '########################################'
emit ''
emit 'System IP=""'
emit 'Domain=""'
if [ "$panelinstall" = 1 ]; then
emit 'Panel Subdomain=""'
elif [ "$nodeinstall" = 1 ]; then
emit 'Node Subdomain=""'
fi
exec 3>&- # close output file
...is a lot simpler (and also more efficient to run) than something that forces use of a heredoc even when it's not called for, like:
cat >"$InfoFile" <<EOF
########################################
# System Information #
########################################
System IP=""
Domain=""
$(if [ "$panelinstall" = 1 ]; then
echo 'Panel Subdomain=""'
elif [ "$nodeinstall" = 1 ]; then
echo 'Node Subdomain=""'
fi)
EOF
Note how we use just <<EOF, not <<'EOF'; leaving out the quotes lets you do parameter expansions, command substitutions, &c inside the heredoc.
This question already has answers here:
bash & jq: add attribute with object value
(2 answers)
Passing variable in jq to filter with select fails
(1 answer)
Closed 5 days ago.
I'm simply trying to replace an objects value in a json file with an array, using jq in a bash script.
The json file (truncated) looks like this:
{
"objects": {
"type": "foo",
"host": "1.1.1.1",
"port": "1234"
}
}
I want to replace the host objects value with an array of different values, so it looks like this:
{
"objects": {
"type": "foo",
"host": ["1.1.1.1","2.2.2.2"],
"port": "1234"
}
}
I tested around with this script. The Input comes from a simple, comma separated string which I convert into a proper json array (which seems to work).
But I'm not able replace the value with the array.
#!/bin/bash
objectshost="1.1.1.1,2.2.2.2"
objectshost_array=$(jq -c -n --arg arg $objectshost '$arg|split(",")')
jq --arg value "$objectshost_array" '.objects.host = $value' ./test.json > ./test.json.tmp
The best I ended up with, is this:
{
"objects": {
"type": "foo",
"host": "[\"1.1.1.1\",\"2.2.2.2\"]",
"port": "1234"
}
}
The result seems to be some logical result, as the script simply replaces the value with the arrays string. But it's not what I expected to get. ;)
I found some similar questions, but all of them were dealing with replacing values in existing arrays or key/value pairs, but my problem seems to be the conversion from a single value to an array.
Can somebody please push me into the right direction? Or should I forget about jq and threat the json file as a simple text file?
Thanks in advance,
André
It would work with a conditional assignment from arguments:
jq '
.objects.host = (
.objects.host |
if type == "array"
then .
else [ . ]
end + $ARGS.positional
)
' input.json --args 1.2.3.4 2.2.2.2 4.4.4.4
Or the same as a stand-alone jq script; which is more readable and maintainable:
myJQScript:
#!/usr/bin/env -S jq --from-file --args
.objects.host = (
.objects.host |
if type == "array"
then .
else [ . ]
end + $ARGS.positional
)
Make it executable:
chmod +x myJQScript
Run it with arguments to add array entries to host
$ ./myJQScript 1.2.3.4 2.2.2.2 4.4.4.4 < input.json
{
"objects": {
"type": "foo",
"host": [
"1.1.1.1",
"1.2.3.4",
"2.2.2.2",
"4.4.4.4"
],
"port": "1234"
}
}
You can do it with a single jq command:
#!/bin/sh
objectshost="1.1.1.1,2.2.2.2"
jq --arg value "$objectshost" '.objects.host = ($value / ",")' ./test.json > ./test.json.tmp
This has the added benefit of not requiring Bash arrays, but can be used with any shell.
If you already have a JSON array, you must use --argjson and not --arg. --arg always creates a variable of type string, --argjson however parses the value as JSON entity.
#!/bin/bash
objectshost="1.1.1.1,2.2.2.2"
objectshost_array=$(printf '%s\n' "$objectshost" | jq -c 'split(",")')
jq --argjson value "$objectshost_array" '.objects.host = $value' ./test.json > ./test.json.tmp
I want output in the below JSON array format with command on linux/bash platform . Can anybody help
data in text file
test:test
test1:test1
test4:test4
Expecting output:
{array :
[
{test:test},
{test1:test1},
{test4:test4}
]
}
Using jq command line JSON parser:
<file jq -Rs '{array:split("\n")|map(split(":")|{(.[0]):.[1]}?)}'
{
"array": [
{
"test": "test"
},
{
"test1": "test1"
},
{
"test4": "test4"
}
]
}
The options Rs let jq read the whole file as one string.
The script splits this string into pieces in order to have the expected format.
Assuming you want actual JSON output:
$ jq -nR '{array: (reduce inputs as $line ([]; . + [$line | split(":") | {(.[0]):.[1]}]))}' input.txt
{
"array": [
{
"test": "test"
},
{
"test1": "test1"
},
{
"test4": "test4"
}
]
}
Creating a JSON works best with a dedicated JSON tool that can also make sense of raw text.
xidel is such a tool.
XPath:
xidel -s input.txt -e '{"array":x:lines($raw) ! {substring-before(.,":"):substring-after(.,":")}}'
(x:lines($raw) is a shorthand for tokenize($raw,'\r\n?|\n'), which creates a sequence of all lines.)
XQuery:
xidel -s input.txt --xquery '{"array":for $x in x:lines($raw) let $a:=tokenize($x,":") return {$a[1]:$a[2]}}'
See also this Xidel online tester.
I am currently writing a script which should make it more easy for me to build some RPMs using mock.
The plan is to make it possible to add values for the mock (and therefor rpmbuild) --define parameter.
The error I get if I add such a define value is
ERROR: Bad option for '--define' ("dist). Use --define 'macro expr'
When I execute the script with as simple as ./test.sh --define "dist .el7" the "debug" output is as follows:
/usr/bin/mock --init -r epel-7-x86_64 --define "dist .el7"
If I copy this and execute it in the shell directly it is actually working. Does anybody have an idea why this is the case?
My script can be cut down to the following:
#!/bin/sh
set -e
set -u
set -o pipefail
C_MOCK="/usr/bin/mock"
MOCK_DEFINES=()
_add_mock_define() {
#_check_parameters_count_strict 1 ${#}
local MOCK_DEFINE="${1}"
MOCK_DEFINES+=("${MOCK_DEFINE}")
}
_print_mock_defines_parameter() {
if [ ${#MOCK_DEFINES[#]} -eq 0 ]; then
return 0
fi
printf -- "--define \"%s\" " "${MOCK_DEFINES[#]}"
}
_mock_init() {
local MOCK_DEFINES_STRING="$(_print_mock_defines_parameter)"
local MOCK_PARAMS="--init"
MOCK_PARAMS="${MOCK_PARAMS} -r epel-7-x86_64"
[ ! "${#MOCK_DEFINES_STRING}" -eq 0 ] && MOCK_PARAMS="${MOCK_PARAMS} ${MOCK_DEFINES_STRING}"
echo "${C_MOCK} ${MOCK_PARAMS}"
${C_MOCK} ${MOCK_PARAMS}
local RC=${?}
if [ ${RC} -ne 0 ]; then
_exit_error "Error while mock initializing ..." ${RC}
fi
}
while (( ${#} )); do
case "${1}" in
-s|--define)
shift 1
_add_mock_define "${1}"
;;
esac
shift 1
done
_mock_init
exit 0
After asking this question a coworker I was pointed to this question on unix stackexchange: Unix Stackexchange question
The way this problem was solved can be broken down to following lines:
DEFINES=()
DEFINES+=(--define "dist .el7")
DEFINES+=(--define "foo bar")
/usr/bin/mock --init -r epel-7-x86_64 "${DEFINES[#]}"
Just in case somebody else stumbles upon this kind of issue.
I am trying to run through huge amount of data. The problem is when I pass 100 objects in array it works perfectly fine but moment I keep 150 or more it starts failing .
Example :--
DBQuery.shellBatchSize = 100000 ;
permissibleCars = [ "C:1456797:665","C:146:5722","C:145:57805","C:146:6070","C:14:60908"]
db.getCollection('contracts').aggregate([
{$match:
{ "methods.name": "image",
"methods.status": "ACTIVE",
container: {"$in": permissibleCars},
Class : "Download"
} },
{"$group" : {_id:"$container", count:{$sum:1}}}],
{ allowDiskUse: true}
);
This will work perfectly fine till the limit in permissibleCars is low say 100 but the moment it crosses 150 or so it starts failing randomly with below error.
2017-08-16T21:30:35.101+0000 E QUERY [thread1] SyntaxError: unterminated string literal #(shell):1:4091
2017-08-16T21:30:35.132+0000 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:6
2017-08-16T21:30:35.162+0000 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:2
2017-08-16T21:30:35.193+0000 E QUERY [thread1] ReferenceError: permissibleCars is not defined :
Now since it runs fine it cannot be syntax issue .
Anyway to get this fixed so that I can pass larger number of variables. I am running this through shell .
for((i=0; i < ${#arr[#]}; i+=batchsize))
do
set display=lastline
IFS=,
part=( "${arr[#]:i:batchsize}" )
{ echo "DBQuery.shellBatchSize = $contracts_count ; "; cat query/container_count_tmp.js; } > query/container_count.js
sed -i "2i permissibleCars = [ ${part[*]} ]" query/container_count.js
mongo mngdb-test-02:27068/test_db -u test_user -p test123 < query/container_count.js >> output/container_count.txt
done
Array Declation :--
distinct_array=`sed ':a;N;$!ba;s/\n/ /g' output/userdistinct.txt`
declare -a arr=($distinct_array)
echo " Total Number of Distinct Ids Stored in Array ${#arr[#]}"
batchsize=150
Any help will be highly appreciated.
Note :-- I checked the page mongodb $in limit not much of information .
Have uploaded the sample data at for testing and to replicate the issue . https://drive.google.com/file/d/0ByHEfbo541jIYlJhSGJIdElCODQ/view?usp=sharing
Regards,
This is not a MongoDB limitation, but if you are using POSIX standard sed implementation, the byte length limit is 8192 bytes. That would explains why you have a syntax error due to the array string has been truncated.
https://www.gnu.org/software/sed/manual/html_node/Limitations.html
For a workaround, use perl instead of sed:
perl -ni -e "print; print 'permissibleCars = [ ${part[*]} ]' if $. == 2" query/container_count.js