Can memcache tell you how much memory it is using? - ruby-on-rails

Can memcache tell you how much memory it is using in total?
How about for a particular key?

And on the command line, you could do this:
echo "stats" | nc 127.0.0.1 11211 | grep bytes

In php, but I'm sure you are able to translate to RoR:-
echo "You are using " . $memcache->getstats()["bytes"] . " of storage ";
echo "out of " . $memcache->getstats()["limit_maxbytes"];
See http://php.net/manual/en/memcache.getstats.php

Related

grep pattern match with begin and end

I have the following text (single line) returned from a call to an API:
data=$(gcloud dns record-sets list --zone=production-internal | grep proj-name-name-dp)
echo $data
proj-name-name-dp.int.proj-name.abc.title.com. CNAME 300 proj-name-name-dp.int.proj-name.abc.title.com.
However I would like to get just proj-name-name-dp.int.proj-name.abc.title.com
Everything from the dot after com should not be stored in data variable.
grep -o didn't help.
Any help is appreciated.
Thanks
If you are ok with awk then could you please try following.
data=$(gcloud dns record-sets list --zone=production-internal | awk '/proj-name-name-dp/{sub(/\.com.*/,".com")} 1')

Formatting nmap output

I have an nmap output looking like this
Nmap scan report for 10.90.108.82
Host is up (0.16s latency).
PORT STATE SERVICE
80/tcp open http
|_http-title: Did not follow redirect to https://10.90.108.82/view/login.html
I would like the output to be like
10.90.108.82 http-title: Did not follow redirect to https://10.90.108.82/view/login.html
How can it be done using grep or any other means?
You can use the following nmap.sh script like that:
<nmap_command> | ./nmap.sh
nmap.sh:
#!/usr/bin/env sh
var="$(cat /dev/stdin)"
file=$(mktemp)
echo "$var" > "$file"
ip_address=$(head -1 "$file" | rev | cut -d ' ' -f1 | rev)
last_line=$(tail -1 "$file" | sed -E "s,^\|_, ,")
printf "%s%s\n" "$ip_address" "$last_line"
rm "$file"
If you do not mind using a programming language, check out this code snippet with Python:
import nmapthon as nm
scanner = nm.NmapScanner('10.90.108.82', ports=[80], arguments='-sS -sV --script http-title')
scanner.run()
if '10.90.108.82' in scanner.scanned_hosts(): # Check if host responded
serv = scanner.service('10.90.108.82', 'tcp', 80)
if serv is not None: # Check if service was identified
print(serv['http-title'])
Do not forget to execute pip3 install nmapthon.
I am the author of the library, feel free to have a look here
Looks like you want an [nmap scan] output to be edited and displayed as you wish. Try bash scripting, code a bash script and run it.
Here's an link to a video where you might find an answer to your problem:
https://youtu.be/lZAoFs75_cs
Watch the video from the Time Stamp 1:27:17 where the creator briefly describes how to cut-short an output and display it as we wish.
If you require, I could code an bash script to execute an cut-shorted version of the output given by an nmap scan.

How to show the output in Geneos Active console using Toolkit Plugin

I'm new with Geneos and would like to know how to show the output of our existing script which is previously used in Nagios. We're planning to use the toolkit plugin and not sure what will be the commands to use to be able to see result in active console.
Requirement - check the log if there is session timeout and it will alert OK if grep is equal 20 then Warning alert if less or greater than 20.
Output in Geneos:
column_title - TIMEOUT CHECK, STATUS
row_result - THE_FILE, OK: Session Timeout is 20
Here's our sample script:
#!/bin/ksh
OK=0
WARNING=1
CRITICAL=2
THE_FILE=/target/directory/web.txt
TIMEOUT=`grep "<session-timeout>" $THE_FILE | awk -F'>' '{print $2}' | awk -F'>' '{print $1}'
if [$TIMEOUT -eq 20 ]; then
echo "OK: Session Timeout is $TIMEOUT"
exit $OK
else
echo "WARNING: Session Timeout is $TIMEOUT"
exit $WARNING
fi
Thanks!
You can use the same script (adding the header) and adding it to Geneos. But I highly recommend you to use a FKM sampler, and check this log file using Geneos directly.
Hope this helps you.

Ruby: Getting the configured region on RedHat/CentOS

I have a Rails application running on a CentOS 7 machine. I would like to read the system-configured region (Olson time zone) into a string (e.g. 'Europe/Stockholm', 'US/Eastern', etc).
I'm not interested in results that differ depending on whether daylight saving is active - 'CET' and 'CEST' and stuff like that won't do it for me. I've dug around Time and TZInfo but can't seem to find anything fitting.
Any ideas?
EDIT: It turns out Rails has no native way of getting the Olson time zone - one would have to do it in the shell, something like this. Neikos' suggestion below is shorter but doesn't work for me because my machine is a modified version of CentOS 7 which is stripped of timedatectl but is perhaps useful for someone else.
EDIT 2: I ended up calling the following shell script from within ruby:
#!/bin/bash
checksum=`md5sum /etc/localtime | cut -d ' ' -f1`
find /usr/share/zoneinfo/ -type f | xargs md5sum 2> /dev/null |
grep "^$checksum" | sed "s,.*/usr/share/zoneinfo/,," |
sort -n | head -n1
Since you are on CentOS 7 you could use something akin to timedatectl | grep Time zone and parse the timezone out of that.
This is obviously not portable to non-systemd systems.
As a not so pretty oneliner:
`timedatectl | grep "Time zone"`.split(/:|\(/)[1].strip

Awk processing and parsing

Trying to use STRICTLY awk to solve a little issue and I can't wrap myself around the solution:
# more connections
0x828ac008 127.0.0.1:5152 127.0.0.1:1387 2000
0x82cc28f8 10.4.4.63:1435 10.4.4.72:22 1132
0x82ec1de0 10.4.4.63:1524 50.28.90.36:8080 3248
# awk -F":" '/[0-9]/{print $1,$2}' connections | awk '!/^127/{print "nslookup "$4}'
nslookup 127.0.0.1
nslookup 10.4.4.72
nslookup 50.28.90.36
I am looking for a streamlined method to just parse out anything aside from loopbacks ^127 and the 10. addresses in my netblock. Yes, I know I can use sed, grep, cut, etc, but I'm hoping to see how someone else would do this in awk. Its more of a learning curve/a-ha! thing
Clarifying: Output would omit 10.x.x.x and 127.x.x.x
Output would be
nslookup 50.28.90.36
I tried awk !/^127\.|^10\./ but I couldn't get it to ignore the values.
This awk should work:
awk -F "[: ]+" 'NR>1 && !($4 ~ /^(127|10)\./){print "nslookup", $4}' connections
OUTPUT:
nslookup 50.28.90.36
Anubhava offers a great solution by setting Field Separators. Here is an alternate way of doing it using the default FS:
Using split function:
awk '$3!~/^(127|10)/{split($3,ip,/:/);print "nslookup",ip[1]}' connections
Using sub function:
awk '$3!~/^(127|10)/{sub(/:.*/,"",$3);print "nslookup", $3}' connections
Also a bash way of doing it:
while read -ra lines; do
[[ ! ${lines[2]} =~ "^(127|10).*" ]] && echo "nslookup ${lines[2]%:*}";
done < connections

Resources