Unable to fetch logs from multiple servers using PsLogList - batch-file

I am using psloglist to get the eventlogs from remote computers. It works fine for one remote computer but doesn't work for multiple computers.
Works fine:
psloglist - \\Server1 -h 4 system -i 7036 -o "Service Control Manager" > c:\temp\logs.txt
Doesn't work:
psloglist - \\Server1,Server2 -h 4 system -i 7036 -o "Service Control Manager" > c:\temp\logs.txt
Please suggest.

(for %%a in ( Server1 Server2 ) do (
psloglist - \\%%a -h 4 system -i 7036 -o "Service Control Manager"
)) > c:\temp\logs.txt
Iterate over the list of servers

Related

Update Local SQL Server with a Bash Script

Question:
What is the correct format to use in my bash script to be able to run the -Q option?
Case: Update local database from S3 every night to run reports on our on-premise server
Code:
#!/bin/bash
#get latest file from S3
BACKUP_MARKETING=`aws s3 ls [some_folder]/[some_subfolder]/ --recursive | sort | tail -n 1 | awk '{print $4}'`
#download the file locally
aws s3 cp s3://[some_folder]/$BACKUP_MARKETING /var/opt/mssql/backup/marketing
#get the file name
BAK_MARKETING=`find [folder]/ -type f -name "*.bak"`
#drop the database to avoid conflicts from not backing it up
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q 'DROP DATABASE [db_name]'
#restore the database
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q RESTORE DATABASE "[db_name]" FROM DISK = "/var/opt/mssql/backup/$BAK_MARKETING" WITH MOVE "[db_name]" TO "/var/opt/mssql/data/[db_name].MDF", MOVE "[db_name]_log" TO "/var/opt/mssql/data/[db_name].LDF"
Error
Sqlcmd: 'DATABASE" "[db_name]" "FROM" "DISK" "=" "/var/opt/mssql/backup/marketing/[db_name].bak" "WITH" "MOVE" "[db_name]" "TO" "/var/opt/mssql/data/[db_name].MDF," "MOVE" "[db_name]_log" "TO" "/var/opt/mssql/data/[db_name].LDF': Unexpected argument. Enter '-?' for help.
Apparently I had to concatenate my variables on the SQL command. Here is the working version plus I added the REPLACE option to it
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q 'RESTORE DATABASE [db_name] FROM DISK = "/var/opt/mssql/backup/'**$BAK_FILE**'" WITH REPLACE, MOVE "[db_name]" TO "/var/opt/mssql/data/[db_name].MDF", MOVE "[db_name]_Log" TO "/var/opt/mssql/data/[db_name].LDF"'
Could you not use the -i Option instead?
I had some problems as well using Q, so i replaced it with -i and placed the code within a .sql file instead.
I ended up with;
SET SQLusername=sa
SET SQLpassword=password
SET SQLserver=dnsnameorIp
SET SQLdatabase=databasename
sqlcmd -U %SQLusername% -P %SQLpassword% -S %SQLserver% -d %SQLdatabase% -i mycode.sql -o outputResult.txt

Retrieve rows in DB corresponding to particular ID using kubectl

I am trying to fetch the no. of rows for a particular ID using kubectl but instead getting some extra data.
Command:
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]"
Actual Output of above command:
Defaulting container name to abc-db.
Use 'kubectl describe pod/abc-db-0 -n cicd' to see all of the containers in this pod.
(0 rows)
Expected Output:
(0 rows)
Could anyone please let me know what I am doing wrong here?
Note:
The first 2 lines always comes when we login to the DB manually but in output I only want (0 rows)
The first two lines are output by kubectl exec because the Pod has multiple containers. It is sort of a warning that it picked the first one, which might not be the one you wanted use.
You can specify the target container in your command (-c containername):
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -c abc-db -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]"
Or you can redirect the standard error with kubectl ... 2>/dev/null (os specific):
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -c -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]" 2>/dev/null

BCP with input file

I would like to export data with BCP
Below is my command
bcp queryout -i "test.sql" -o"myTable.csv" -S "server\Db" -E /c /t, -T
test.sql has the SQL statemement. I need to keep the SQL in a file as the statement is rather long
I have tested the SQL to be returning values in the management studio
But I get the below errors in the command prompt
Copy direction must be either 'in', 'out' or 'format'.
I have also tried the below variations without much luck
bcp out -i "test.sql" -o"myTable.csv" -S "server\Db" -E /c /t, -T
bcp -i "test.sql" out -o"myTable.csv" -S "server\Db" -E /c /t, -T
bcp -i "test.sql" queryout -o"myTable.csv" -S "server\Db" -E /c /t, -T
The -i specifies input data file. The query has to be part of the command line in the queryout. The bcp tool is quite ancient and didn't improve over the years much.
You might use different tools to do the expert. BCP shines when you need to import data, but exporting can be done efficiently using many other tools.
To output in csv format you can use sqlcmd see How to export data as CSV format from SQL Server using sqlcmd?

How to execute multiple queries using psql command from bash shell?

I need to execute postgresql queries from command line using psql -c command.
For every psql command, it opens a new tcp connection to connect to the database server and execute query which is a overhead for large number of queries.
Currently I can execute single query like this:
psql -U postgres -h <ip_addr> -c "SELECT * FROM xyz_table;"
When I tried to execute multiple queries as below, but only the last query got executed.
psql -U postgres -h <ip_addr> -c "SELECT * FROM xyz_table; SELECT * FROM abc_table;"
Can anyone help me and tell me the proper way to do it?
-c processes only one command. Without it however psql expects commands to be passed into standard input, e.g.:
psql -U postgres -h <ip_addr> <database_name> << EOF
SELECT * FROM xyz_table;
SELECT * FROM abc_table;
EOF
Or by using echo and pipes.
at least from 9.6.2 this approach works as well:
psql -c "select now()" -c "select version()" -U postgres -h 127.0.0.1
now
2017-12-26 20:25:45.874935+01
(1 row)
version
PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.3.1-14ubuntu2) 5.3.1 20160413, 64-bit
(1 row)
Using echo and a pipe to fit it on a single line:
echo 'SELECT * FROM xyz_table; \n SELECT * FROM abc_table' | psql -U postgres
The --file parameter executes a file's content
psql -U postgres -h <ip_addr> -f "my_file.psql"
All the output will be sent to standard output
http://www.postgresql.org/docs/current/static/app-psql.html

Error: -bash: check_nrpe: command not found

I am using Nagios XI. I issued following command from the Nagios Server:
nagiossrv root [libexec] > check_nrpe -H 128.19.5.131 -t 30 -c check_users -w 5 -c 10
It is giving me following error:
-bash: check_nrpe: command not found
I have also added the IP address of the Nagios server (nagiossrv) to the /usr/local/nagios/etc/nrpe.cfg file at the host's (128.19.5.131) side.
What is the issue?
After a little research, I got above error resolved. Just had to enter the command as follows:
nagiossrv root [libexec] > /usr/local/nagios/libexec/check_nrpe -H 128.19.5.131 -t 30 -c check_users -a '-w 5 -c 10'
And the output would look like:
USERS OK - 1 users currently logged in |users=1;5;10;0

Resources