I have a variable env and I want to get it in telegraf. I use [[inputs.exec]]
My code:
[[inputs.exec]]
commands = ["echo $val_env"]
data_format = "value"
data_type = "string"
name_override = "value_A"
But I can't get this value $val_env. I also test another way such as [echo "${val_env}"] or ["$val_env"], ... but as same.
How I can get value variable env in commands [[input.exec]] telegraf.
The variable val_env has to be exported to the environment that is running telegraf.
Here's an example, with most of the noise removed for brevity, and I've added to my config exactly what you described:
$ grep '^\[\[inputs.exec' -A4 /etc/telegraf/telegraf.conf
[[inputs.exec]]
commands = ["echo $val_env"]
data_format = "value"
data_type = "string"
name_override = "value_A"
$ export val_env='Tai Do'
$ telegraf
2022-12-22T11:51:31Z I! Using config file: /etc/telegraf/telegraf.conf
2022-12-22T11:51:31Z I! Starting Telegraf 1.25.0
2022-12-22T11:51:31Z I! Available plugins: 227 inputs, 9 aggregators, 26 processors, 21 parsers, 57 outputs, 2 secret-stores
2022-12-22T11:51:31Z I! Loaded inputs: cpu disk diskio exec kernel mem processes swap system
2022-12-22T11:51:31Z I! Loaded aggregators:
2022-12-22T11:51:31Z I! Loaded processors:
2022-12-22T11:51:31Z I! Loaded secretstores:
2022-12-22T11:51:31Z I! Loaded outputs: exec file
...
value_A,host=***,user=root value="Tai Do" 1671709900000000000
Related
Steps followed to installed SNMP manager and agent on ec2
sudo apt-get update
sudo apt-get install snmp snmp-mibs-downloader
sudo apt-get update
sudo apt-get install snmpd
I opened sudo nano /etc/snmp/snmp.conf and commented the following line:
#mibs :
Then I went into the configuration file and modified file as below:
sudo nano /etc/snmp/snmpd.conf
Listen for connections from the local system only
agentAddress udp:127.0.0.1:161 <--- commented this part.
Listen for connections on all interfaces (both IPv4 and IPv6)
agentAddress udp:161,udp6:[::1]:161 <--remove the comment from this line to make it work.
using below command I can get snmp data
snmpwalk -v 2c -c public 127.0.0.1 .
From inside docker container as well I can get the data
snmpwalk -v 2c -c public host.docker.internal .
Docker-compose:
telegraf_snmp:
image: telegraf:1.22.1
container_name: telegraf_snmp
restart: always
depends_on:
- influxdb
networks:
- analytics
extra_hosts:
- "host.docker.internal:host-gateway"
# ports:
# - "161:161/udp"
volumes:
- /mnt/telegraf/snmp:/var/lib/telegraf
- ./etc/telegraf/snmp/:/etc/telegraf/snmp/
env_file:
- secrets.env
environment:
INFLUXDB_URL: http://influxdb:8086
command:
--config-directory /etc/telegraf/snmp/telegraf.d
--config /etc/telegraf/snmp/telegraf.conf
links:
- influxdb
logging:
options:
max-size: "10m"
max-file: "3"
Telegraf Input conf:
[[inputs.snmp]]
## Agent addresses to retrieve values from.
## format: agents = ["<scheme://><hostname>:<port>"]
## scheme: optional, either udp, udp4, udp6, tcp, tcp4, tcp6.
## default is udp
## port: optional
## example: agents = ["udp://127.0.0.1:161"]
## agents = ["tcp://127.0.0.1:161"]
## agents = ["udp4://v4only-snmp-agent"]
# agents = ["udp://127.0.0.1:161"]
agents = ["udp://host.docker.internal:161"]
## Timeout for each request.
timeout = "15s"
## SNMP version; can be 1, 2, or 3.
version = 2
## SNMP community string.
community = "public"
## Agent host tag
# agent_host_tag = "agent_host"
## Number of retries to attempt.
retries = 3
## The GETBULK max-repetitions parameter.
# max_repetitions = 10
## SNMPv3 authentication and encryption options.
##
## Security Name.
# sec_name = "myuser"
## Authentication protocol; one of "MD5", "SHA", or "".
# auth_protocol = "MD5"
## Authentication password.
# auth_password = "pass"
## Security Level; one of "noAuthNoPriv", "authNoPriv", or "authPriv".
# sec_level = "authNoPriv"
## Context Name.
# context_name = ""
## Privacy protocol used for encrypted messages; one of "DES", "AES", "AES192", "AES192C", "AES256", "AES256C", or "".
### Protocols "AES192", "AES192", "AES256", and "AES256C" require the underlying net-snmp tools
### to be compiled with --enable-blumenthal-aes (http://www.net-snmp.org/docs/INSTALL.html)
# priv_protocol = ""
## Privacy password used for encrypted messages.
# priv_password = ""
## Add fields and tables defining the variables you wish to collect. This
## example collects the system uptime and interface variables. Reference the
## full plugin documentation for configuration details.
[[inputs.snmp.field]]
oid = "RFC1213-MIB::sysUpTime.0"
name = "uptime"
[[inputs.snmp.field]]
oid = "RFC1213-MIB::sysName.0"
name = "source"
is_tag = true
[[inputs.snmp.table]]
oid = "IF-MIB::ifTable"
name = "interface"
inherit_tags = ["source"]
[[inputs.snmp.table.field]]
oid = "IF-MIB::ifDescr"
name = "ifDescr"
is_tag = true
Telegraf logs:
Cannot find module (IF-MIB): At line 1 in (none)
IF-MIB::ifTable: Unknown Object Identifier: exit status 2
2022-09-09T10:10:09Z I! Starting Telegraf 1.22.1
2022-09-09T10:10:09Z I! Loaded inputs: snmp
2022-09-09T10:10:09Z I! Loaded aggregators:
2022-09-09T10:10:09Z I! Loaded processors:
2022-09-09T10:10:09Z I! Loaded outputs: file influxdb_v2
2022-09-09T10:10:09Z I! Tags enabled: host=7a38697f4527
2022-09-09T10:10:09Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"7a38697f4527", Flush Interval:10s
2022-09-09T10:10:09Z E! [telegraf] Error running agent: could not initialize input inputs.snmp: initializing table interface: translating: MIB search path: /root/.snmp/mibs:/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf
Cannot find module (IF-MIB): At line 1 in (none)
IF-MIB::ifTable: Unknown Object Identifier: exit status 2
2022-09-09T10:10:11Z I! Starting Telegraf 1.22.1
2022-09-09T10:10:11Z I! Loaded inputs: snmp
2022-09-09T10:10:11Z I! Loaded aggregators:
2022-09-09T10:10:11Z I! Loaded processors:
2022-09-09T10:10:11Z I! Loaded outputs: file influxdb_v2
2022-09-09T10:10:11Z I! Tags enabled: host=7a38697f4527
2022-09-09T10:10:11Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"7a38697f4527", Flush Interval:10s
2022-09-09T10:10:11Z E! [telegraf] Error running agent: could not initialize input inputs.snmp: initializing table interface: translating: MIB search path: /root/.snmp/mibs:/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf
Cannot find module (IF-MIB): At line 1 in (none)
IF-MIB::ifTable: Unknown Object Identifier: exit status 2
But in telegraf I get above error.
I checked the mibs directory using ls /usr/share/snmp/mibs
I cannot find IF-MIB file here even after installing
$ sudo apt-get install snmp-mibs-downloader
$ sudo download-mibs
How can I resolve this issue ? Do I need to follow some additional steps ?
SNMP Plugin in telegraf should able to pull the data from SNMP
I try to monitor Redis health with Zabbix agent and want to parse output for redis-cli INFO command:
$ redis-cli INFO
# Server
redis_version:4.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9435c3c2879311f3
...
with shell script and compose JSON file with subset of info values:
{
"redis_version": "4.0.9",
"used_memory": 100,
"used_memory_rss": 200
}
But I have an error when I try to echo parsed values to console. This is my demo script:
#!/bin/bash
FILE_ECHO="/tmp/test_out.txt"
FILE_REDIS="/tmp/test_redis.txt"
echo "redis_version:4.0.9" >${FILE_ECHO}
/usr/bin/redis-cli INFO >${FILE_REDIS}
ECHO_VERSION=$(grep "redis_version" "${FILE_ECHO}")
REDIS_VERSION=$(grep "redis_version" "${FILE_REDIS}")
echo "{\"node\": \"${ECHO_VERSION}\"}"
echo "{\"node\": \"${REDIS_VERSION}\"}"
I expect that output will be:
{"node": "redis_version:4.0.9"}
{"node": "redis_version:4.0.9"}
in both cases, but actually I have:
{"node": "redis_version:4.0.9"}
"}node": "redis_version:4.0.9
This is my test_redis.txt.
In jupyter notebook, following gcloud commands work with bang(!) but not with %%bash
import os
PROJECT = 'mle-1234'
REGION = 'us-central1'
BUCKET = 'mle-1234'
# for bash
os.environ['PROJECT'] = PROJECT
os.environ['BUCKET'] = BUCKET
os.environ['REGION'] = REGION
os.environ['TFVERSION'] = '1.14.0' # Tensorflow version
# Set GCP Project and Region
%%bash
gcloud config set project $PROJECT
gcloud config set compute/region $REGION
gcloud config list
I get this error message when I execute the last snippet above with %%bash
File "<ipython-input-16-f93912dbcc34>", line 3
gcloud config set project $[PROJECT]
^
SyntaxError: invalid syntax
However, project and region values get set with same lines of code but by removing %%bash and prefixing (!) with all gcloud commands.
# Set GCP Project and Region
!gcloud config set project $PROJECT
!gcloud config set compute/region $REGION
!gcloud config list
Result with using (!)
Updated property [core/project].
Updated property [compute/region].
[compute]
region = us-central1
zone = us-central1-a
[core]
account = my-service-account#mle-1234.iam.gserviceaccount.com
disable_usage_reporting = False
project = mle-1234
What could be the reason for this behavior?
%%bash
%%bash is considered part of the Built-in magic commands. Run cells with bash in a subprocess. This is a shortcut for %%script bash. You can combine code from multiple kernels into one notebook. For example:
%%HTML
%%python2
%%python3
%%ruby
%%perl
implementation:
%%bash
factorial()
{
if [ "$1" -gt "1" ]
then
i=`expr $1 - 1`
j=`factorial $i`
k=`expr $1 \* $j`
echo $k
else
echo 1
fi
}
input=5
val=$(factorial $input)
echo "Factorial of $input is : "$val
! command
Starting a code cell with a bang character, e.g. !, instructs jupyter to treat the code on that line as an OS shell command
!cat /etc/os-release | grep VERSION
Output:
VERSION="16.04 LTS (Xenial Xerus)"
VERSION_ID="16.04"
Answer: since you are using gcloud commands, Jupyter will interpret those as OS shell commands; and therefore, using !glcoud will work.
I am using a simple salt state to send (file.managed) and execute (cmd.run) a shell script on a minion/target. No matter what exit or return value the shell script sends, the salt master is interpreting the result as successful.
I tried using cmd.script, but keep getting a permission denied error on the temp version of the file under /tmp. Filesystem is not mounted with noexec so we can't figure out why it won't work.
For cmd.run, stdout in the job output shows the failed return code and message but Salt still says Success. Running the script locally on the minion reports the return/exit code as expected.
I tried adding stateful: True into the cmd.run block and formatted the key value pairs at the end of the shell script as demonstrated in the docs.
Running against 2 minion target, 1 fail 1 succeed. Both report Result as True but correctly populate Comment with my key value pair.
I've tried YES/NO, TRUE/FALSE, 0/1 - nothing works.
The end of my shell script, formatted as shown in the docs.
echo Return_Code=${STATUS}
# exit ${STATUS}
if [[ ${STATUS} -ne 0 ]]
then
echo ""
echo "changed=False comment='Failed'"
else
echo ""
echo "changed=True comment='Success'"
fi
The SLS block:
stop_oracle:
cmd.run:
- name: {{scriptDir}}/{{scriptName}}{{scriptArg}}
- stateful: True
- failhard: True
SLS output from Successful minion:
----------
ID: stop_oracle
Function: cmd.run
Name: /u01/orastage/oraclepsu/scripts/oracle_ss_wrapper.ksh stop
Result: True
Comment: Success
Started: 14:37:44.519131
Duration: 18930.344 ms
Changes:
----------
changed:
True
pid:
26195
retcode:
0
stderr:
stty: standard input: Inappropriate ioctl for device
stdout:
Script running under ROOT
Mon Jul 1 14:38:03 EDT 2019 : Successful
Return_Code=0
SLS output from Failed minion:
----------
ID: stop_oracle
Function: cmd.run
Name: /u01/orastage/oraclepsu/scripts/oracle_ss_wrapper.ksh stop
Result: True
Comment: Failed
Started: 14:07:14.153940
Duration: 38116.134 ms
Changes:
Output from shell script run locally on fail target:
[oracle#a9tvdb102]:/home/oracle:>>
/u01/orastage/oraclepsu/scripts/oracle_ss_wrapper.ksh stop
Mon Jul 1 15:29:18 EDT 2019 : There are errors in the process
Return_Code=1
changed=False comment='Failed'
Output from shell script run locally on success target:
[ /home/oracle ]
oracle#r9tvdo1004.giolab.local >
/u01/orastage/oraclepsu/scripts/oracle_ss_wrapper.ksh stop
Mon Jul 1 16:03:18 EDT 2019 : Successful
Return_Code=0
changed=True comment='Success'
I'm trying to generate a env.ts file in my Circleci config file with my circleci environment variables. I tried the code below:
steps:
- run:
name: Setup Environment Variables
command:
echo "export const env = {
jwtSecret: ${jwtSecret},
gqlUrl: ${gqlUrl},
engineAPIToken: ${engineAPIToken},
mongodb: ${mongodb},
mandrill: ${mandrill},
gcpKeyFilename: ${gcpKeyFilename},
demo: ${demo},
nats: ${NATS},
usernats: ${usernats},
passnats: ${passnats} };" | base64 --wrap=0 > dist/env.ts
but it outputs this:
#!/bin/sh -eo pipefail
# Unable to parse YAML
# mapping values are not allowed here
# in 'string', line 34, column 24:
# jwtSecret: ${jwtSecret},
# ^
#
# -------
# Warning: This configuration was auto-generated to show you the message above.
# Don't rerun this job. Rerunning will have no effect.
false
Exited with code 1
I forgot to write a pipe after command: |
steps:
- run:
name: Setup Environment Variables
command: |
echo "export const env = {
jwtSecret: '${jwtSecret}',
gqlUrl: '${gqlUrl}',
engineAPIToken: '${engineAPIToken}',
mongodb: '${mongodb}',
mandrill: '${mandrill}',
gcpKeyFilename: '${gcpKeyFilename}',
demo: ${demo}, // it's a boolean
nats: '${NATS}',
usernats: '${usernats}',
passnats: '${passnats}'
};" > dist/env.ts
Note: Also I had forgotten to add '' around my variables.