Merge lp:~lazypower/charms/precise/nrpe/fix-lp-1287393 into lp:charms/nrpe

Proposed by Charles Butler
Status: Merged
Approved by: Peter Petrakis
Approved revision: 30
Merged at revision: 28
Proposed branch: lp:~lazypower/charms/precise/nrpe/fix-lp-1287393
Merge into: lp:charms/nrpe
Diff against target: 206 lines (+189/-1)
2 files modified
hooks/nrpe-relation-joined (+1/-1)
lib/net.sh (+188/-0)
To merge this branch: bzr merge lp:~lazypower/charms/precise/nrpe/fix-lp-1287393
Reviewer Review Type Date Requested Status
Charles Butler (community) Approve
Matt Bruzek (community) Approve
Peter Petrakis Pending
Review via email: mp+209137@code.launchpad.net

Description of the change

replaces charm-helpers-sh call with nslookup

https://codereview.appspot.com/69980048/

To post a comment you must log in.
Revision history for this message
Charles Butler (lazypower) wrote :

Reviewers: mp+209137_code.launchpad.net,

Message:
Please take a look.

Description:
replaces charm-helpers-sh call with nslookup

https://code.launchpad.net/~lazypower/charms/precise/nrpe/fix-lp-1287393/+merge/209137

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/69980048/

Affected files (+3, -1 lines):
   A [revision details]
   M hooks/nrpe-relation-joined

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: <email address hidden>

Index: hooks/nrpe-relation-joined
=== modified file 'hooks/nrpe-relation-joined'
--- hooks/nrpe-relation-joined 2012-07-05 22:22:37 +0000
+++ hooks/nrpe-relation-joined 2014-03-03 22:05:22 +0000
@@ -7,7 +7,7 @@
  . hooks/nrpe.common.bash

  # NRPE requires IP
-address=$(ch_get_ip $(relation-get private-address))
+address=`nslookup $(unit-get private-address) | grep Add | grep -v '#' |
cut -f 2 -d ' '`

  juju-log "Adding $address to allowed_hosts."

Revision history for this message
Matt Bruzek (mbruzek) wrote :

I have a comment/question about the new command you are using.

https://codereview.appspot.com/69980048/diff/1/hooks/nrpe-relation-joined
File hooks/nrpe-relation-joined (right):

https://codereview.appspot.com/69980048/diff/1/hooks/nrpe-relation-joined#newcode10
hooks/nrpe-relation-joined:10: address=`nslookup $(unit-get
private-address) | grep Add | grep -v '#' | cut -f 2 -d ' '`
I ran this nslookup command on my own system and I believe the two greps
are cancelling each out.

$ nslookup 192.168.168.13
Server: 127.0.1.1
Address: 127.0.1.1#53

** server can't find 13.168.168.192.in-addr.arpa.: NXDOMAIN

$ nslookup 192.168.168.13 | grep Add
Address: 127.0.1.1#53

$ nslookup 192.168.168.13 | grep Add | grep -v '#'
(no match)

Are you sure this command works in all cases? Am I using the command
incorrectly here, or should we consider refining the command to work in
this case too?

https://codereview.appspot.com/69980048/

Revision history for this message
Matt Bruzek (mbruzek) wrote :

This change needs more information.

Add a comment to make it more clear what IP address the code is trying to get. The new code gets the unit address rather than the relation address (which I realize may be the same one).

Since there appears to be a possiblity of getting an empty address. Either fall back to the old way of getting the address or log a warning, or error if the address resolves to an empty string.

review: Needs Information
29. By Charles Butler

Embed lib/net.sh instead of rewriting the world

Revision history for this message
Charles Butler (lazypower) wrote :

Embedded net.sh with an addition of returning $HOST if found.

Refactored out the cowboy nslookup that fails on local. Give it another go please

30. By Charles Butler

remove stray return 1 for debugging

Revision history for this message
Charles Butler (lazypower) wrote :
Revision history for this message
Matt Bruzek (mbruzek) wrote :

+1 LGTM

review: Approve
Revision history for this message
Charles Butler (lazypower) wrote :

Merged

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/nrpe-relation-joined'
2--- hooks/nrpe-relation-joined 2012-07-05 22:22:37 +0000
3+++ hooks/nrpe-relation-joined 2014-03-19 12:23:25 +0000
4@@ -3,7 +3,7 @@
5
6 set -e
7
8-. /usr/share/charm-helper/sh/net.sh
9+. lib/net.sh
10 . hooks/nrpe.common.bash
11
12 # NRPE requires IP
13
14=== added directory 'lib'
15=== added file 'lib/net.sh'
16--- lib/net.sh 1970-01-01 00:00:00 +0000
17+++ lib/net.sh 2014-03-19 12:23:25 +0000
18@@ -0,0 +1,188 @@
19+#!/bin/sh
20+
21+##
22+# Copyright 2011 Marco Ceppi <marco@ceppi.net>
23+#
24+# This file is part of Charm Helpers.
25+#
26+# Charm Helpers is free software: you can redistribute it and/or modify
27+# it under the terms of the GNU General Public License as published by
28+# the Free Software Foundation, either version 3 of the License, or
29+# (at your option) any later version.
30+#
31+# Charm Helpers is distributed in the hope that it will be useful,
32+# but WITHOUT ANY WARRANTY; without even the implied warranty of
33+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34+# GNU General Public License for more details.
35+#
36+# You should have received a copy of the GNU General Public License
37+# along with Charm Helpers. If not, see <http://www.gnu.org/licenses/>.
38+##
39+
40+##
41+# Globally overridable settings. Make sure to set them before sourcing
42+# this file.
43+CH_WGET_ARGS=${CH_WGET_ARGS:-"-q --content-disposition"}
44+
45+##
46+# Get File
47+# Retrives a file and compares the file to a hash
48+#
49+# param FILE URL or Path to file
50+# param HASH URL, Path, or HASH string for file's hash
51+#
52+# return filepath|null
53+##
54+ch_get_file()
55+{
56+ local FILE=${1:-""}
57+ local HASH=${2:-""}
58+ local CH_DOWNLOAD_DIR=${CH_DOWNLOAD_DIR:-"`mktemp -d /tmp/ch-downloads.XXXXXX`"}
59+
60+ if [ `ch_is_url "$FILE"` ]; then
61+ wget $CH_WGET_ARGS --directory-prefix="$CH_DOWNLOAD_DIR/" "$FILE"
62+ FILE=$CH_DOWNLOAD_DIR/$(ls -tr $CH_DOWNLOAD_DIR|head -n 1)
63+ fi
64+
65+ if [ ! -f "$FILE" ]; then
66+ return 2
67+ fi
68+
69+ if [ -z "$HASH" ];then
70+ #echo "Warning, no has specified. The file will be downloaded but not cryptographically checked!" > 2
71+ echo "$FILE"
72+ return 0
73+ elif [ `ch_is_url "$HASH"` ]; then
74+ local HASHNAME=$(basename $HASH)
75+ wget $CH_WGET_ARGS "$HASH" -O /tmp/$HASHNAME
76+ HASH=$(cat /tmp/$HASHNAME | awk '{ print $1 }')
77+ elif [ -f "$HASH" ]; then
78+ HASH=$(cat "$HASH" | awk '{ print $1 }')
79+ fi
80+
81+ local HASH_TYPE=$(ch_type_hash $HASH)
82+
83+ if [ -z "$HASH_TYPE" ]; then
84+ return 3
85+ else
86+ local FILE_HASH=$(${HASH_TYPE}sum $FILE | awk '{ print $1 }')
87+
88+ if [ "$FILE_HASH" != "$HASH" ]; then
89+ return 4
90+ fi
91+ fi
92+
93+ echo "$FILE"
94+ return 0
95+}
96+
97+##
98+# Hash Type
99+# Determine, using best approximation, if the hash is valid and what type
100+# of hashing algorithm was used
101+#
102+# param HASH
103+#
104+# return type|false
105+##
106+ch_type_hash()
107+{
108+ local DIRTY="$1"
109+
110+ case $DIRTY in
111+ *[![:xdigit:]]* | "" )
112+ echo ""
113+ return 1
114+ ;;
115+ * )
116+ case ${#DIRTY} in
117+ 32 )
118+ echo md5
119+ ;;
120+ 40 )
121+ echo sha1
122+ ;;
123+ 64 )
124+ echo sha256
125+ ;;
126+ esac
127+ ;;
128+ esac
129+}
130+
131+##
132+# Is URL?
133+# Checks if the string passed is a valid URL (http(s), ftp)
134+#
135+# param URL The URL to be checked
136+#
137+# return boolean
138+##
139+ch_is_url()
140+{
141+ local DIRTY="$1"
142+
143+ case "$DIRTY" in
144+ "http://"* | "https://"* | "ftp://"*)
145+ echo true
146+ return 0
147+ ;;
148+ *)
149+ return 1
150+ ;;
151+ esac
152+}
153+
154+##
155+# Is IP?
156+# Checks if the string passed is an IP address
157+#
158+# param IP The IP addressed to be checked
159+#
160+# return boolean
161+##
162+ch_is_ip()
163+{
164+ local DIRTY="$1"
165+ local IP=$(echo $DIRTY | awk -F. '$1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ')
166+
167+ if [ -z "$IP" ]; then
168+ return 1
169+ else
170+ echo true
171+ return 0
172+ fi
173+}
174+
175+##
176+# Get IP
177+# Returns the first IP match to the hostname provided
178+#
179+# param HOSTNAME Host for which to retrieve an IP
180+#
181+# return IP|false
182+##
183+ch_get_ip()
184+{
185+ local HOST="$1"
186+ #So, if there's multiple IP addresses, just grab the first
187+ # for now.
188+ if [ `ch_is_ip "$HOST"` ]; then
189+ echo "$HOST"
190+ return 0
191+ fi
192+
193+ local CHECK_IP=$(host -t A $HOST | awk 'NR==1{ print $4 }')
194+
195+ if [ ! `ch_is_ip "$CHECK_IP"` ]; then
196+ # Try a dig, why not?
197+ CHECK_IP=$(dig +short $HOST | awk 'NR==1{ print $1 }')
198+
199+ if [ ! `ch_is_ip "$CHECK_IP"` ]; then
200+ return 1
201+ fi
202+ fi
203+
204+ echo $CHECK_IP
205+ return 0
206+}

Subscribers

People subscribed via source and target branches