Comment out/delete blocks in large amount of files - regex

I'm attempting to write something that can process Nagios config files containing a block of text, and either add # to the start of each line, or delete the block. (To function as a kind of mass check removal in Nagios itself).
Ex:
define service {
service_description Service 1
use Template 1
host_name Host 1
check_command Command A
}
define service {
service_description Service 2
use Template 1
host_name Host 1
check_command Command B
}
define service {
service_description Service 3
use Template 1
host_name Host 1
check_command Command C
}
Would need to change to this (or equivalent):
define service {
service_description Service 1
use Template 1
host_name Host 1
check_command Command A
}
#define service {
# service_description Service 2
# use Template 1
# host_name Host 1
# check_command Command B
#}
define service {
service_description Service 3
use Template 1
host_name Host 1
check_command Command C
}
Is there a way to regex match the block between "define service {" and "}", and containing either "Service 2" or "Command "B", and append/delete the block via sed/awk/perl, etc?
Thanks.

sed '
# take each paragraph one by one
/define service {/,/}/{
# inside paragraphe, add each line (one at a time) in buffer
H
# if not the end of paragraphe, delete the line (from output) (and cycle to next line)
/}/!d
# empty current line (the last of paragraphe) and swap with the whole buffer
s/.*//;x
# if it contain Service2 (and Command B on next line) goto to label comm
/Service2/ b comm
/Command B/ b comm
# goto label head (so no Service2 nor command b in paragraphe)
b head
:comm
# comment each line of paragraphe (multiline with \n as new line)
s/\n/&#/g
: head
# remove first character (a new line due to use of H and not h on first line)
s/.//
}
# default behaviour that print the content
' YourFile
Self commented

Here's a regex that creates the match you want:
/define service\s\{.*(Service 2|Command B).*\}/s
You can test it here. I have no experience with sed, awk or perl, so I will not make any attempt to create a replacement.

sed -n '/define service {/,/}/{:1;N;/\}/!b 1;/Command B\|Service 2/{s/\n/\n#/g;s/^/#/g;p;d;b 1};p;d;b 1}' file_name
This is how it works
picks the block within braces;keeps appending to pattern space till a closing brace is found;searches the patter space for the two key words; if found then appends a # in front of every new line in pattern space and prints the content. Clears the pattern space and repeats the provess

Related

Edit/Match a string n lines below some specific match in Bash?

I have a complex problem. Below is the ndxconfig.ini file I want to Edit
# /etc/ndxconfig.ini will override this file
# if APP_ID is added in service propery, service discovery will be using marathon;
# HOST/PORT specified will override values retrieved from marathon
[MARATHON]
HOSTS = {{ ','.join(groups['marathon'])}}
PORT = 8080
PROTOCOL = http
SECRET = SGpQIcjK2P7RYnrdimhhhGg7i8MdmUqwvA2JlzbyujFS4mR8M88svI7RfNWt5rnKy4WHnAihEZmmIUb940bnlYmnu47HdUHE
[MYSQL]
; APP_ID = /neon/infra/mysql
HOST = {{keepalived_mysql_virtual_ip}}
PORT = 3306
SECRET = tIUFN1rjjDBdEXUsOJjPEtdieg8KhwTzierD48JsgDeYc84DD6Uy5a6kzHfKolq1MNS1DKwlSqxENk33UulJd9DPHPzYCxFm
I want to change specifically marathon protocol conf from http to https. Not other's protocol conf. I have to match PROTOCOL = http 3 lines below the [MARATHON] line. I researched and couldn't find any solution. There's only 1 line below sed solutions.
One idea stuck mine was somehow specially grep [MARATHON] and 3 lines below and tail 1 line. I don't know.
How can fix this? Please Help.
Solution found here
sed '/\[MARATHON\]/{N;N;N;s/http/https/;}' <file>
If you have python available, you can use crudini:
crudini --set ndxconfig.ini MARATHON protocol https
This screams for ed, which treats the file as a whole, not processing it a line at a time like sed (And also doesn't depend on the availability of the non-posix -i extension to sed for in-place editing of files):
ed -s ndxconfig.ini <<'EOF'
/MARATHON/;/^$/ s/^PROTOCOL = http$/PROTOCOL = https/
w
EOF
This will replace the PROTOCOL line in the block that starts with a line matching MARATHON and ending with a blank line. The protocol entry can be the second, third, fourth, etc. line; doesn't matter. If the protocol is already https, it won't do anything (Except print a question mark)

Implementing a 'for loop' within user_data provisioner file

I am currently using a template_file to provision user_data into an aws_launch_configuration, like so:
resource "aws_launch_configuration" "launch_config" {
...
user_data = "${data.template_file.init.rendered}"
}
data "template_file" "init" {
template = "${file("router-init.sh.tpl")}"
vars {
hub_ip_addresses = "${join(",", aws_instance.gridHub.*.private_ip)}"
}
}
I am feeding in a variable (i.e. hub_ip_addresses) into the router-init.sh.tpl file, and in this file I am making use of the argument like so:
`#!/bin/sh
...
IFS=',' read -r -a array <<< "$hub_ip_addresses"
for element in "${array[#]}"
do
#do stuff with $element
done
Basically, I am splitting the string based on a delimiter, and then looping through each ip address in the array.
This bash script works fine when I run it on my local machine -- however, when terraform executes it, it throws a error:
* data.template_file.init: data.template_file.init: failed to render : parse error at 13:25: expected expression but found invalid sequence "#"
I'm supposing the '#' symbol is causing an issue. Is there a reason why this is so? Do I need to escape it with a '\' ?
EDIT: Not sure if related to this issue, but in the preceeding line in the bash script, IFS=',' read -r -a array <<< "$hub_ip_addresses", the <<< seems to be causing everything else that follows to look as if they are inside a comment (i.e. greyed out as if it was within a quotation mark ').)
You need to escape the $ characters in your template by doubling them up or Terraform will attempt to interpolate them as the input variables to the template.
The template docs cover this briefly although the example given is for inline templates rather than for all templates, including those that are loaded with the file() function.
So something like:
#!/bin/sh
...
IFS=',' read -r -a array <<< "$hub_ip_addresses"
for element in "$${array[#]}"
do
#do stuff with $$element
done

cat lines into file after regex

I want to add the info below into the file usr/local/nagios/etc/hosts.cfg but want to do it just below ##company in the hosts.cfg file. My setup script will contain the info that needs to be added
I have spend hours trying to get sed to just add a line into a file after a marker but to no avail
define host{
use linux-box
host_name $host_name
alias $alias
address $ip
parents $parent
notification_period 24x7
notification_interval 5
}
Previously I used
cat <> /path /filename
EOT
but now I need to do it in specif places in the file
Given the following file:
# some content
###company
If I run the following command:
sed -i 's/###company/&\ndefine host {\nuse host\nhost_name HOSTNAME/' file
Now, the contents of file are:
# some content
###company
define host {
use host
host_name HOSTNAME
Is this what you're looking for?

Monitor web service with Nagios check_http

I am trying to monitor a web service with check_http plug-in of Nagios.
The url I am trying to monitor includes url parameter.
And it turns out that check_http plugin will ignore the url parameter when checking.
Herewith my configuration.
'check_http' command definition
define command{
command_name check_http
command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
}
define service{
use local-service ; Name of service template to use
host_name pear
service_description HTTP
check_command check_http!-u http://pear.com/total?eId=12345&env=abcde
notifications_enabled 0
}
Try replacing the value passed into -u with a relative path instead of the full URL.
In this example the hostname (-H) will be supplied by $HOSTADDRESS$ which is taken from the address field of the pear host definition.
The value passed into the -u parameter should be a relative path, for example: /total?eId=12345&env=abcde.
We'll add the -u to the check_http_with_args command definition so we don't have to pass it as part of our parameters in our service definition.
define host {
host_name pear
alias pear
address pear.com
use linux-server
contact_groups admins
notification_interval 0
notification_period 24x7
notifications_enabled 1
register 1
}
define command{
command_name check_http_with_args
command_line $USER1$/check_http -H $HOSTADDRESS$ -u $ARG1$
}
define service {
service_description pear_total_http
use generic-service
check_command check_http_with_args!/total?eId=12345&env=abcde
host_name pear
contact_groups admins
notification_interval 0
notification_period 24x7
notifications_enabled 1
flap_detection_enabled 1
register 1
}
In the end, the command that Nagios execute should get translated into something that looks like this:
/usr/local/nagios/libexec/check_http -H pear.com -u /total?eId=12345&env=abcde
You can try executing the above from the command line to make sure it works for you.
Note: Replace the path to check_http with the actual path corresponding to your installation location on your Nagios server.
Relevant parts of the check_http man pages we're referencing:
-H, --hostname=ADDRESS
Host name argument for servers using host headers (virtual host)
Append a port to include it in the header (eg: example.com:5000)
...
-u, --url=PATH
URL to GET or POST (default: /)
Source: https://www.monitoring-plugins.org/doc/man/check_http.html
EDIT:
To answer your questions from the comment, the -k or --header= will allow you to pass in headers.
-k, --header=STRING
Any other tags to be sent in http header. Use multiple times for additional headers
Source: https://www.monitoring-plugins.org/doc/man/check_http.html
So to specify an Accept header, I'd modify the following to look like:
define command{
command_name check_http_with_args
command_line $USER1$/check_http -H $HOSTADDRESS$ -u "$ARG1$" -k "$ARG2$"
}
define service {
service_description pear_total_http
use generic-service
check_command check_http_with_args!/total?eId=12345&env=abcde!Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
host_name pear
contact_groups admins
notification_interval 0
notification_period 24x7
notifications_enabled 1
flap_detection_enabled 1
register 1
}
...adding -k "$ARG2$" to the command_line of the command definition and adding the Accept: <MIME types> (for example Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8) to the check_command of the service definition.
Also, I wrapped $ARG1$ of -u "$ARG1$" part of the command_line in double quotation marks as I suspect the & in eId=12345&env=abcde is causing the shell to think the end of the command is terminated just before the &. Wrapping the parameter in double quotes should make it see the whole string as one whole argument.

Scripting the cisco banner with Net::Appliance::Session

Has anyone ran into this issue? When the script gets to the banner text the script just hangs.
I am using Net::Appliance::Session
Here is the error I get in debug. The rest of the script inserts code perfectly. I did test what I read about adding a # to the banner for each line. Same result.
banner login +
[ 4.092880] tr nope, doesn't (yet) match (?-xism:[\/a-zA-Z0-9._\[\]-]+ ?(?:\(config[^)]*\))? ?[#>] ?$)
[ 4.093124] du SEEN:
banner login +
[ 4.093304] tr nope, doesn't (yet) match (?-xism:[\/a-zA-Z0-9._\[\]-]+ ?(?:\(config[^)]*\))? ?[#>] ?$)
[ 4.305872] du SEEN:
Enter TEXT message. End with the character '+'
[ 4.306121] tr nope, doesn't (yet) match (?-xism:[\/a-zA-Z0-9._\[\]-]+ ?(?:\(config[^)]*\))? ?[#>] ?$)
We had an issue when accessing the device : 10.49.216.74
The reported error was : read timed-out at /usr/lib/perl5/site_perl/5.10.0/Net/CLI/Interact/Transport/Wrapper/Net_Telnet.pm line 35
Here is a snip of code.
my $session_obj = Net::Appliance::Session->new(
host => $ios_device_ip,
transport => 'Telnet',
personality => 'ios',
timeout => 60,
);
#interace
$session_obj->set_global_log_at('debug');
eval {
# try to login to the ios device, ignoring host check
$session_obj->connect(
username => $ios_username,
password => $ios_password,
#SHKC => 0
);
# get our running config
$version_info = $session_obj->begin_privileged;
$session_obj->cmd('conf t');
$session_obj->cmd('line con 0');
$session_obj->cmd('exec-character-bits 8');
$session_obj->cmd('international');
$session_obj->cmd('line vty 0 4');
$session_obj->cmd('exec-character-bits 8');
$session_obj->cmd('international');
$session_obj->cmd('line vty 5 15');
$session_obj->cmd('exec-character-bits 8');
$session_obj->cmd('international');
$session_obj->cmd('exit');
$session_obj->cmd('no banner login');
$session_obj->cmd('banner login +');
$session_obj->cmd('*************************************************************************');
$session_obj->cmd('* test *');
$session_obj->cmd('* *');
$session_obj->cmd('*************************************************************************');
$session_obj->cmd('+');
$session_obj->cmd('no banner MOTD');
$session_obj->cmd('banner motd +');
$session_obj->cmd('*************************************************************************');
$session_obj->cmd('* test *');
$session_obj->cmd('* *');
$session_obj->cmd('*************************************************************************');
$session_obj->cmd('+');
$session_obj->cmd('exit');
$session_obj->cmd('write memory');
$session_obj->end_privileged;
# close down our session
$session_obj->close;
};
If you look at the regexp that matches the prompt before sending a new command you'll see that it requires a specific string that closely matches user, privileged or config mode of a router.
When you send the banner login + command you get the Enter TEXT message. End with the character '+' followed by blank line from a router (instead of Router(config)# that your script expects. After a while it just times out since there is no match for the regexp.
The easiest solution is to try to send the whole banner in one command. Try concatenating your banner with a \r in one string and sending it as a one command that looks like (note the double quotes):
$session_obj->cmd("banner login + line1 \r line2 \r line3\r +");
Took way too long to figure this out... spaces are not your friend.
$session_obj->cmd("banner login + \rline1\rline2\rline3\r+");
Example with my orginal problem:
$session_obj->cmd('*************************************************************************\r* test *\r* *\r*************************************************************************');