Permalink
Browse files

* Try harder to get FQDN hostname for the host where monit is running.

  The hostname in the $HOST variable which is used in the mail sender
  may thus change. Thanks to Sergey B Kirpichev for patch.



git-svn-id: http://monit.googlecode.com/svn/trunk@342 808b68a2-07de-11de-a1f0-819f45317607
  • Loading branch information...
1 parent dcafa2f commit ec4c3782e6ab408eded533da3ef963e3b1085f36 mmonit@tildeslash.com committed Mar 23, 2011
Showing with 44 additions and 4 deletions.
  1. +5 −0 CHANGES.txt
  2. +1 −1 p.y
  3. +1 −3 sendmail.c
  4. +28 −0 util.c
  5. +9 −0 util.h
View
@@ -23,6 +23,10 @@ Version 5.2.5
* Fix bug #32583: Multiple SIP OPTIONS messages use the same header data.
Thanks to Hugh Waite for patch.
+* Try harder to get FQDN hostname for the host where monit is running.
+ The hostname in the $HOST variable which is used in the mail sender
+ may thus change. Thanks to Sergey B Kirpichev for patch.
+
* AIX: Fix the time display which was off by GMT difference. Thanks to
Helen Chen for report.
@@ -32,6 +36,7 @@ Version 5.2.5
+
Version 5.2.4
NEW FEATURES AND FUNCTIONS:
View
2 p.y
@@ -1866,7 +1866,7 @@ static void preparse() {
/*
* Get the localhost name
*/
- if (gethostname(localhost, sizeof(localhost)) < 0)
+ if (Util_getfqdnhostname(localhost, sizeof(localhost)))
snprintf(localhost, STRLEN, "%s", LOCALHOST);
/* Set instance incarnation ID */
View
@@ -135,9 +135,7 @@ int sendmail(Mail_T mail) {
Util_getRFC822Date(NULL, now, STRLEN);
- if(Run.mail_hostname || gethostname(S.localhost, sizeof(S.localhost)) < 0) {
- snprintf(S.localhost, sizeof(S.localhost), "%s", Run.mail_hostname?Run.mail_hostname:LOCALHOST);
- }
+ snprintf(S.localhost, sizeof(S.localhost), "%s", Run.mail_hostname ? Run.mail_hostname : Run.localhostname);
do_status(&S);
View
28 util.c
@@ -88,6 +88,10 @@
#include <sys/types.h>
#endif
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
+
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
@@ -2006,6 +2010,30 @@ void Util_stringbuffer(Buffer_T *b, const char *m, ...) {
}
+int Util_getfqdnhostname(char *buf, unsigned len) {
+ int status;
+ char hostname[STRLEN];
+ struct addrinfo hints, *info;
+
+ if (gethostname(hostname, sizeof(hostname))) {
+ LogError("%s: Error getting hostname -- %s\n", prog, STRERROR);
+ return -1;
+ }
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_CANONNAME;
+ if ((status = getaddrinfo(hostname, NULL, &hints, &info))) {
+ LogError("%s: Cannot translate '%s' to FQDN name -- %s\n", prog, hostname, gai_strerror(status));
+ snprintf(buf, len, "%s", hostname); // fallback to gethostname()
+ } else
+ snprintf(buf, len, "%s", info->ai_canonname);
+
+ return 0;
+}
+
+
/* ----------------------------------------------------------------- Private */
View
9 util.h
@@ -516,4 +516,13 @@ char *Util_portDescription(Port_T p, char *buf, int bufsize);
void Util_stringbuffer(Buffer_T *b, const char *m, ...);
+/**
+ * Returns the FQDN hostname or fallback to gethostname() output
+ * @param buf the character array for hostname
+ * @param len the length of buf
+ * @return zero on success
+ */
+int Util_getfqdnhostname(char *buf, unsigned len);
+
+
#endif

0 comments on commit ec4c378

Please sign in to comment.