Adding a column (first column) to csv file with service names and the status - csv

I am using below code to redirect service names and status on my centos box to a csv file:-
service --status-all |
grep -E running\|stopped |
awk '{print $1,","," ",$NF,","," "}' >> "$HOME/MyLog/running_services_$HOSTNAME.csv"
Output for service --status-all command on my box is like:-
httpd is running
Now, I basically need the first column in this csv file as the ip address of the centos box. I can get the IP using this:-
ifconfig | sed -En 's/127.0.0.1//;s/.inet (addr:)?(([0-9].){3}[0-9])./\2/p'
Output of above command is:- 192.168.0.38 (IP address of my host).
Current CSV Output is like:- ServiceName Status
I would need the output to be as:- IP Address ServiceName Status
I need to know how can I insert the first column in this CSV which will have the IP address for each of the rows - populated with service names and status. Any pointers on this?
Can I print this command output as first column using awk?
Thanks.

Capture the IP address first, and give the value to awk
ip=$( ifconfig | sed -En 's/127.0.0.1//;s/.inet (addr:)?(([0-9].){3}[0-9])./\2/p' )
service --status-all | awk -v ip="$ip" -v OFS="," '/running|stopped/ {print ip, $1, $NF}' >> "$HOME/MyLog/running_services_$HOSTNAME.csv"
Note that you don't need grep: awk can do that job.

Related

bash script continues to show "does not exist. file "

I'm running this bash scripts to read a CSV File which contains almost 10 lines with 05 columns:
Variables Global
DATE=$(date) #Set DATE equal to the output of running the shell command date
HOSTNAME=$(hostname) #Set HOSTNAME equal to the output of the hostname command
CSV_FILE='/scrpt/jobs_aix01.csv'
CSV_DELIMITADOR=','
#Variables
contador_sql=0
cadena_sql="error"
## Script
while IFS=\, read col_job_name col_os col_server col_user col_script
do
#echo "$col_job_name $col_os $col_server $col_user $col_script"
ls -l $col_script
done < "$CSV_FILE"
The script must read the CSV file and run the command ls -l but I get "does not exist. file "
Any ideas?
note: the files exist.
regards
unix jose

Using ncat inside bash script

I'm doing simple http-server on bash with ncat, which should receive some json in POST and then compile from received json another one with variables replacement. How to make the script running forward after first loop?
"Server A" sends some json to "Server B". "Server B" - is a docker container with some script inside. "Server C" is the destination. The script is below
The first part, starting ncat on 5000 port, redirecting all input to the file
#!/bin/bash
ncat -lk 5000 > /tmp/ncat.txt
Replacing variables from income json
id=$(echo $(date +%s))
name=$(cat /tmp/ncat.txt | grep receiver | jq -r '.alerts[].labels.alertname')
status_current=$(cat /tmp/ncat.txt | grep receiver | jq -r '.status')
info=$(cat /tmp/ncat.txt | grep receiver | jq -r '.commonAnnotations.message')
Sending recompiled json to "Server C"
curl http://server-c/webhook -X POST -d '
{
"id":"'"$id"'",
"name":"'"$name"'",
"status_current":"'"$status_current"'",
"info":"'"$info"'"
}'
The question is: after receiving the first request, the scripts stops to work. How to make a loop there? Like: ncat listening the port, receiving json, recompiling vars, executing curl, ncat listening the port...
Second question: when i'm sending dummy json from "Server A" (simple curl -X POST -d ''), the second step oof the script will not execute until i break (ctrl+c) the curl request.
Thanks in advance.

How to parse echo json value?

I want to parse a value or convert a value into json format.
I have no idea how to do it.
echo -e $(kubectl get pods "test-pod" -o jsonpath="{range .status.containerStatuses[*]}{.state}"\\n"{end}")
map[running:map[startedAt:2019-06-07T00:51:34Z]]
map[running:map[startedAt:2019-06-07T00:51:40Z]]
map[running:map[startedAt:2019-06-07T00:51:44Z]]
map[waiting:map[message:Back-off 5m0s restarting failed container=con4 pod=test-pod_test(609c90e4-88be-11e9-ba5f-fa163e9a67be) reason:CrashLoopBackOff]]
I would like to get only all containers' status like [running, running, running, waiting].
Thanks in advance.
You can achieve it using the jq and keys[] command in jq. The following will be the command to use:
kubectl get pods kube-dns-86f4d74b45-khd4z -n kube-system -o json | jq -r '.status.containerStatuses[].state | keys[]'
The above command will give the following output of all container running or waiting or any state

Store Codeship build ID in variable using GREP

I am trying to use a grep to search a JSON output, I used a curl command to return the data from a particular codeship build and I want to use GREP to store said ID value in a variable. However after I run the command and try to echo out the value of the variable its blank.
Below are the commands:
export API_KEY=abc123
export PROJECT_ID=123456
export LAST_BUILD_ID=$(curl -s https://codeship.com/api/v1/projects/$PROJECT_ID.json?api_key=$API_KEY | grep -Eo '"builds":\[{"id":\d+' | grep -Eo --color=never '\d+' | tail -1)
export LAST_BUILD_URL=$(echo "https://codeship.com/api/v1/builds/$LAST_BUILD_ID/restart.json?api_key=$API_KEY")
My response : never use grep nor regex to parse json.
Instead, use a proper json parser.
In shell, take a look to jq.
Example, adapt it a bit :
#!/bin/bash
API_KEY=abc123
PROJECT_ID=123456
html=$(curl -s https://codeship.com/api/v1/projects/$PROJECT_ID.json?api_key=$API_KEY)
LAST_BUILD_ID=$(jq '.builds | .[] | .never' <<< "$html") # just guessing
LAST_BUILD_URL=$(echo "https://codeship.com/api/v1/builds/$LAST_BUILD_ID/restart.json?api_key=$API_KEY")
Note
If you provide the JSON, I will be able to be more specific with the jq command

How do I name the output textfile to YYYYMMDD based on the system date?

How do I name the output textfile to YYYYMMDD based on the system date?
sqlcmd -S DataBBB -i c:\scripts\followup.sql
-o %DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%.txt -s ; -W -u
Now the output text file is 01_31_2012.txt.
How can I change it to 2012_01_31.txt?
Tried it under Windows 7 Premium (German), may be dependent upon OS and Local Time Format.
sqlcmd -S DataBBB -i c:\scripts\followup.sql
-o %date:~-4%_%date:~3,2%_%date:~0,2%.txt -s;
You have to edit this part for your system
%date:~-4%_%date:~3,2%_%date:~0,2%.txt
The statement used the command line internal %date% - variable with the extension :~start,length. So you can create the filename with different parts from the date- variable.