diff options
| author | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-10-10 14:32:29 +0000 |
|---|---|---|
| committer | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-10-10 14:32:29 +0000 |
| commit | 65fb13bf23e53b616ff5849dc4ef292d992a57fd (patch) | |
| tree | f061dcb36d3ee127bb3876155d244827989c6c9e /src | |
| parent | 7621affa2fc1c48036f16f3de592a7b377a16a95 (diff) | |
- [ZBX-19] fixed SMS sending (Sasha)
[svn merge svn://svn.zabbix.com/branches/1.4 -r4849:4851]
git-svn-id: svn://svn.zabbix.com/trunk@4852 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
| -rw-r--r-- | src/libs/zbxsms/sms.c | 125 | ||||
| -rw-r--r-- | src/zabbix_server/alerter/alerter.c | 8 |
2 files changed, 80 insertions, 53 deletions
diff --git a/src/libs/zbxsms/sms.c b/src/libs/zbxsms/sms.c index d8e0c3d8..1a984077 100644 --- a/src/libs/zbxsms/sms.c +++ b/src/libs/zbxsms/sms.c @@ -46,7 +46,7 @@ static int write_gsm(int fd, char *str, char *error, int max_error_len) len = strlen(str); - zabbix_log(LOG_LEVEL_DEBUG, "Write [%s]", str); + zabbix_log(LOG_LEVEL_DEBUG, "Write to GSM modem [%s]", str); for ( wlen = 0; wlen < len; wlen += i ) { @@ -67,37 +67,47 @@ static int write_gsm(int fd, char *str, char *error, int max_error_len) return ret; } -static int read_gsm(int fd, const char *expect, char *error, int max_error_len) +int read_gsm(int fd, const char *expect, char *error, int max_error_len, int timeout_sec) { - static char buffer[0xFF]; + static char buffer[0xff]; static char *ebuf = buffer; static char *sbuf = buffer; + char rcv[0xff]; fd_set fdset; + struct timeval tv; - int i, nbytes; + int i, nbytes, len; int ret = SUCCEED; - /* wait for response from modem */ + if( timeout_sec == 0 ) + { + goto check_result; + } + + tv.tv_sec = timeout_sec; + tv.tv_usec = 0; + +/* wait for response from modem */ FD_ZERO(&fdset); FD_SET(fd, &fdset); do { - i = select(fd + 1, &fdset, NULL, NULL, NULL); - if ( -1 == i ) + i = select(fd + 1, &fdset, NULL, NULL, &tv); + if ( i == -1 ) { if ( EINTR == errno ) continue; zabbix_log(LOG_LEVEL_DEBUG, "Error select() for GSM modem. [%s]", strerror(errno)); if ( error ) zbx_snprintf(error,max_error_len, "Error select() for GSM modem. [%s]", strerror(errno)); - return (ret = FAIL); + return FAIL; } - else if ( 1 != i ) + else if ( i == 0 ) /*( 1 != i )*/ { /* Timeout exceeded */ zabbix_log(LOG_LEVEL_DEBUG, "Error during wait for GSM modem."); if ( error ) zbx_snprintf(error,max_error_len, "Error during wait for GSM modem."); - return (ret = FAIL); + goto check_result; } else { @@ -111,8 +121,12 @@ static int read_gsm(int fd, const char *expect, char *error, int max_error_len) ebuf += nbytes; } /* nul terminate the string and see if we got an OK response */ +check_result: *ebuf = '\0'; + zabbix_log(LOG_LEVEL_DEBUG, "Read from GSM modem [%s]", sbuf); + strcpy(rcv, sbuf); + if( '\0' == *expect ) /* empty */ { sbuf = ebuf = buffer; @@ -120,30 +134,34 @@ static int read_gsm(int fd, const char *expect, char *error, int max_error_len) return ret; } - zbx_ltrim(sbuf, "\r\n"); - - for(i = 0 ; i < (ebuf - sbuf) && (sbuf[i] != '\n' && sbuf[i] != '\r'); i++); /* find first '\r' & '\n' */ - - if(i < ebuf - sbuf) sbuf[i++] = '\0'; + do + { + len = ebuf - sbuf; + for( i = 0; i < len && (sbuf[i] != '\n' && sbuf[i] != '\r'); i++ ) + ; /* find first '\r' & '\n' */ -#if defined(DEBUG) - for ( i = 0; i < strlen(buffer); i++ ) zabbix_log(LOG_LEVEL_DEBUG, "%3i[0x%x]", i, buffer[i]); -#endif /* DEBUG */ + if(i < len) + { + sbuf[i++] = '\0'; + } - if (strstr(sbuf, expect) == NULL) - { - zabbix_log(LOG_LEVEL_DEBUG, "Read something unexpected from GSM modem. Expected [%s] instead [%s]", expect, sbuf); - if ( error ) zbx_snprintf(error,max_error_len, "Read something unexpected from GSM modem. Expected [%s]", expect); - ret = FAIL; - } + ret = ( strstr(sbuf, expect) == NULL ) ? FAIL : SUCCEED; - sbuf += i; + sbuf += i; - if(sbuf != buffer) + if ( sbuf != buffer ) + { + memmove(buffer, sbuf, ebuf - sbuf + 1); /* +1 for '\0' */ + ebuf -= sbuf - buffer; + sbuf = buffer; + } + } while( (sbuf < ebuf) && (ret == FAIL) ); + + if ( ret == FAIL && error ) { - memmove(buffer, sbuf, ebuf - sbuf + 1); /* +1 for '\0' */ - ebuf -= sbuf - buffer; - sbuf = buffer; + zbx_snprintf(error, max_error_len, "Expected [%s] received [%s]", + expect, + rcv); } return ret; @@ -152,6 +170,7 @@ static int read_gsm(int fd, const char *expect, char *error, int max_error_len) typedef struct { char *message; const char *result; + int timeout_sec; } zbx_sms_scenario; int send_sms(char *device,char *number,char *message, char *error, int max_error_len) @@ -160,17 +179,18 @@ int send_sms(char *device,char *number,char *message, char *error, int max_error #define ZBX_AT_CTRL_Z "\x1A" zbx_sms_scenario scenario[] = { -/* 0 */ {"\r" ZBX_AT_ESC , "" }, /* Send <ESC> */ -/* 1 */ {"ATE0\r" , "OK" }, /* Turn off echo */ -/* 2 */ {"AT\r" , "OK" }, /* Init modem */ -/* 3 */ {"AT+CMGF=1\r" , "OK" }, /* Switch to text mode */ -/* 4 */ {"AT+CMGS=\"" , NULL }, /* Set phone number */ -/* 5 */ {number , NULL }, /* Write phone number */ -/* 6 */ {"\"\r" , ">" }, /* Set phone number */ -/* 7 */ {message , NULL }, /* Write message */ -/* 8 */ {ZBX_AT_CTRL_Z , "+CMGS: " }, /* Send message */ -/* 9 */ {NULL , "OK" }, /* ^Z */ -/* EOS */ {NULL , NULL } +/* 0 */ {ZBX_AT_ESC , NULL , 0 }, /* Send <ESC> */ +/* 1 */ {"AT+CMEE=2\r" , "OK" , 5 }, /* verbose error values */ +/* 1 */ {"ATE0\r" , "OK" , 5 }, /* Turn off echo */ +/* 2 */ {"AT\r" , "OK" , 5 }, /* Init modem */ +/* 3 */ {"AT+CMGF=1\r" , "OK" , 5 }, /* Switch to text mode */ +/* 4 */ {"AT+CMGS=\"" , NULL , 0 }, /* Set phone number */ +/* 5 */ {number , NULL , 0 }, /* Write phone number */ +/* 6 */ {"\"\r" , "> " , 5 }, /* Set phone number */ +/* 7 */ {message , NULL , 0 }, /* Write message */ +/* 8 */ {ZBX_AT_CTRL_Z , "+CMGS: " , 40 }, /* Send message */ +/* 9 */ {NULL , "OK" , 1 }, /* ^Z */ +/* EOS */ {NULL , NULL , 0 } }; zbx_sms_scenario *step = NULL; @@ -182,11 +202,17 @@ int send_sms(char *device,char *number,char *message, char *error, int max_error int f, ret = SUCCEED; - - if ( -1 == (f = open(device,O_RDWR | O_NOCTTY | O_NDELAY)) ) + zabbix_log( LOG_LEVEL_DEBUG, "In send_sms()"); + + if ( -1 == (f = open(device, O_RDWR | O_NOCTTY | O_NDELAY)) ) { - zabbix_log(LOG_LEVEL_DEBUG, "Error open(%s) [%s]", device, strerror(errno)); - if ( error ) zbx_snprintf(error,max_error_len, "Error open(%s) [%s]", device, strerror(errno)); + zabbix_log(LOG_LEVEL_DEBUG, "Error open(%s) [%s]", + device, + strerror(errno)); + if ( error ) + zbx_snprintf(error,max_error_len, "Error open(%s) [%s]", + device, + strerror(errno)); return FAIL; } fcntl(f, F_SETFL,0); /* Set the status flag to 0 */ @@ -205,7 +231,7 @@ int send_sms(char *device,char *number,char *message, char *error, int max_error options.c_cflag = old_options.c_cflag | CRTSCTS | CS8 | CLOCAL | CREAD; options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_cc[VMIN] = 0; - options.c_cc[VTIME] = 20; + options.c_cc[VTIME] = 1; tcsetattr(f, TCSANOW, &options); @@ -213,22 +239,25 @@ int send_sms(char *device,char *number,char *message, char *error, int max_error { if(step->message) { - if(FAIL == (ret = write_gsm(f, step->message, error, max_error_len))) break; + if ( FAIL == (ret = write_gsm(f, step->message, error, max_error_len)) ) break; } if(step->result) { - if(FAIL == (ret = read_gsm(f, step->result, error, max_error_len))) break; + if( FAIL == (ret = read_gsm(f, step->result, error, max_error_len, step->timeout_sec)) ) break; } } if ( FAIL == ret ) { write_gsm(f, "\r" ZBX_AT_ESC ZBX_AT_CTRL_Z, NULL, 0); /* cancel all */ - read_gsm(f, "", NULL, 0); /* clear buffer */ + read_gsm(f, "", NULL, 0, 0); /* clear buffer */ } tcsetattr(f, TCSANOW, &old_options); close(f); + zabbix_log( LOG_LEVEL_DEBUG, "End of send_sms() [%s]", + ret == SUCCEED ? "SUCCEED" : "FAIL" ); + return ret; } diff --git a/src/zabbix_server/alerter/alerter.c b/src/zabbix_server/alerter/alerter.c index 14d3191e..dde733ef 100644 --- a/src/zabbix_server/alerter/alerter.c +++ b/src/zabbix_server/alerter/alerter.c @@ -63,7 +63,6 @@ int execute_action(DB_ALERT *alert,DB_MEDIATYPE *mediatype, char *error, int max (char *)&env_mediatypeid, (char *)&env_status, (char *)0 }; - zabbix_log( LOG_LEVEL_DEBUG, "In execute_action(%s)", mediatype->smtp_server); @@ -190,7 +189,6 @@ int main_alerter_loop() for(;;) { - zbx_setproctitle("connecting to the database"); DBconnect(ZBX_DB_CONNECT_NORMAL); @@ -230,8 +228,9 @@ int main_alerter_loop() sigaction(SIGALRM, &phan, NULL); /* Hardcoded value */ - /* SMS requires 15s for sending */ + /* SMS uses its own timeouts */ alarm(40); + *error = '\0'; res = execute_action(&alert,&mediatype,error,sizeof(error)); alarm(0); @@ -239,9 +238,8 @@ int main_alerter_loop() { zabbix_log( LOG_LEVEL_DEBUG, "Alert ID [" ZBX_FS_UI64 "] was sent successfully", alert.alertid); - DBexecute("update alerts set status=%d where status=%d and alertid=" ZBX_FS_UI64, + DBexecute("update alerts set status=%d,error='' where alertid=" ZBX_FS_UI64, ALERT_STATUS_SENT, - ALERT_STATUS_NOT_SENT, alert.alertid); } else |
