diff --git a/src/counters.c b/src/counters.c index f05f8ea..2bdeedc 100644 --- a/src/counters.c +++ b/src/counters.c @@ -48,28 +48,30 @@ } -void update_counters(struct session_data *sdata, struct counters *counters, struct config *cfg){ +void update_counters(struct session_data *sdata, struct data *data, struct counters *counters, struct config *cfg){ char buf[MAXBUFSIZE]; #ifdef HAVE_MEMCACHED unsigned long long mc, rcvd; struct counters c; char key[MAX_MEMCACHED_KEY_LEN]; unsigned int flags=0; +#endif if(counters->c_virus + counters->c_duplicate + counters->c_ignore + counters->c_size + counters->c_stored_size <= 0) return; +#ifdef HAVE_MEMCACHED if(cfg->update_counters_to_memcached == 1){ /* increment counters to memcached */ - if(memcached_increment(&(data->memc), MEMCACHED_MSGS_RCVD, strlen(MEMCACHED_MSGS_RCVD), counters->c_rcvd, &mc) == MEMCACHED_SUCCESS){ + if(memcached_increment(&(data->memc), MEMCACHED_MSGS_RCVD, counters->c_rcvd, &mc) == MEMCACHED_SUCCESS){ rcvd = mc; - if(counters->c_virus > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_VIRUS, strlen(MEMCACHED_MSGS_VIRUS), counters->c_virus, &mc); - if(counters->c_duplicate > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_DUPLICATE, strlen(MEMCACHED_MSGS_DUPLICATE), counters->c_duplicate, &mc); - if(counters->c_ignore > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_IGNORE, strlen(MEMCACHED_MSGS_IGNORE), counters->c_ignore, &mc); - if(counters->c_size > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_SIZE, strlen(MEMCACHED_MSGS_SIZE), counters->c_size, &mc); - if(counters->c_stored_size > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_STORED_SIZE, strlen(MEMCACHED_MSGS_STORED_SIZE), counters->c_stored_size, &mc); + if(counters->c_virus > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_VIRUS, counters->c_virus, &mc); + if(counters->c_duplicate > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_DUPLICATE, counters->c_duplicate, &mc); + if(counters->c_ignore > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_IGNORE, counters->c_ignore, &mc); + if(counters->c_size > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_SIZE, counters->c_size, &mc); + if(counters->c_stored_size > 0) memcached_increment(&(data->memc), MEMCACHED_MSGS_STORED_SIZE, counters->c_stored_size, &mc); bzero(&c, sizeof(c)); @@ -88,8 +90,8 @@ } - if(sdata->now - mc > cfg->memcached_to_db_interval && c.c_rcvd > 0 && c.c_rcvd >= rcvd){ - snprintf(buf, SMALLBUFSIZE-1, "%ld", sdata->now); memcached_set(&(data->memc), MEMCACHED_COUNTERS_LAST_UPDATE, strlen(MEMCACHED_COUNTERS_LAST_UPDATE), buf, strlen(buf), 0, 0); + if(sdata->now - mc > (unsigned long long)cfg->memcached_to_db_interval && c.c_rcvd > 0 && c.c_rcvd >= rcvd){ + snprintf(buf, SMALLBUFSIZE-1, "%ld", sdata->now); memcached_add(&(data->memc), "set", MEMCACHED_COUNTERS_LAST_UPDATE, buf, strlen(buf), 0, 0); snprintf(buf, SMALLBUFSIZE-1, "UPDATE `%s` SET `rcvd`=%llu, `virus`=%llu, `duplicate`=%llu, `ignore`=%llu, `size`=%llu, `stored_size`=%llu", SQL_COUNTER_TABLE, c.c_rcvd, c.c_virus, c.c_duplicate, c.c_ignore, c.c_size, c.c_stored_size); @@ -104,21 +106,19 @@ c = load_counters(sdata); - snprintf(buf, SMALLBUFSIZE-1, "%ld", sdata->now); memcached_add(&(data->memc), MEMCACHED_COUNTERS_LAST_UPDATE, strlen(MEMCACHED_COUNTERS_LAST_UPDATE), buf, strlen(buf), 0, 0); + snprintf(buf, SMALLBUFSIZE-1, "%ld", sdata->now); memcached_add(&(data->memc), "add", MEMCACHED_COUNTERS_LAST_UPDATE, buf, strlen(buf), 0, 0); - snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_virus + counters->c_virus); memcached_add(&(data->memc), MEMCACHED_MSGS_VIRUS, strlen(MEMCACHED_MSGS_VIRUS), buf, strlen(buf), 0, 0); - snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_rcvd + counters->c_rcvd); memcached_add(&(data->memc), MEMCACHED_MSGS_RCVD, strlen(MEMCACHED_MSGS_RCVD), buf, strlen(buf), 0, 0); - snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_duplicate + counters->c_duplicate); memcached_add(&(data->memc), MEMCACHED_MSGS_DUPLICATE, strlen(MEMCACHED_MSGS_DUPLICATE), buf, strlen(buf), 0, 0); - snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_ignore + counters->c_ignore); memcached_add(&(data->memc), MEMCACHED_MSGS_IGNORE, strlen(MEMCACHED_MSGS_IGNORE), buf, strlen(buf), 0, 0); - snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_size + counters->c_size); memcached_add(&(data->memc), MEMCACHED_MSGS_SIZE, strlen(MEMCACHED_MSGS_SIZE), buf, strlen(buf), 0, 0); - snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_stored_size + counters->c_stored_size); memcached_add(&(data->memc), MEMCACHED_MSGS_STORED_SIZE, strlen(MEMCACHED_MSGS_STORED_SIZE), buf, strlen(buf), 0, 0); + snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_virus + counters->c_virus); memcached_add(&(data->memc), "add", MEMCACHED_MSGS_VIRUS, buf, strlen(buf), 0, 0); + snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_rcvd + counters->c_rcvd); memcached_add(&(data->memc), "add", MEMCACHED_MSGS_RCVD, buf, strlen(buf), 0, 0); + snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_duplicate + counters->c_duplicate); memcached_add(&(data->memc), "add", MEMCACHED_MSGS_DUPLICATE, buf, strlen(buf), 0, 0); + snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_ignore + counters->c_ignore); memcached_add(&(data->memc), "add", MEMCACHED_MSGS_IGNORE, buf, strlen(buf), 0, 0); + snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_size + counters->c_size); memcached_add(&(data->memc), "add", MEMCACHED_MSGS_SIZE, buf, strlen(buf), 0, 0); + snprintf(buf, SMALLBUFSIZE-1, "%llu", c.c_stored_size + counters->c_stored_size); memcached_add(&(data->memc), "add", MEMCACHED_MSGS_STORED_SIZE, buf, strlen(buf), 0, 0); } } else { #endif - if(counters->c_virus + counters->c_duplicate + counters->c_ignore + counters->c_size + counters->c_stored_size <= 0) return; - snprintf(buf, SMALLBUFSIZE-1, "UPDATE `%s` SET `rcvd`=`rcvd`+%llu, `virus`=`virus`+%llu, `duplicate`=`duplicate`+%llu, `ignore`=`ignore`+%llu, `size`=`size`+%llu, `stored_size`=`stored_size`+%llu", SQL_COUNTER_TABLE, counters->c_rcvd, counters->c_virus, counters->c_duplicate, counters->c_ignore, counters->c_size, counters->c_stored_size); p_query(sdata, buf); diff --git a/src/import.c b/src/import.c index 0b3884b..dfdd6a6 100644 --- a/src/import.c +++ b/src/import.c @@ -110,7 +110,7 @@ counters.c_rcvd = 1; counters.c_size += sdata->tot_len; counters.c_stored_size = sdata->stored_len; - update_counters(sdata, &counters, cfg); + update_counters(sdata, data, &counters, cfg); break; @@ -119,7 +119,7 @@ bzero(&counters, sizeof(counters)); counters.c_duplicate = 1; - update_counters(sdata, &counters, cfg); + update_counters(sdata, data, &counters, cfg); if(data->quiet == 0) printf("duplicate: %s (duplicate id: %llu)\n", data->import->filename, sdata->duplicate_id); break; diff --git a/src/memc.c b/src/memc.c index d65ee96..0e2a148 100644 --- a/src/memc.c +++ b/src/memc.c @@ -217,12 +217,13 @@ } -int memcached_add(struct memcached_server *ptr, char *key, unsigned int keylen, char *value, unsigned int valuelen, unsigned int flags, unsigned long expiry){ +int memcached_add(struct memcached_server *ptr, char *cmd, char *key, char *value, unsigned int valuelen, unsigned int flags, unsigned long expiry){ int len=0; if(memcached_connect(ptr) != MEMCACHED_SUCCESS) return MEMCACHED_FAILURE; - snprintf(ptr->buf, MAXBUFSIZE-1, "add %s %d %ld %d \r\n", key, flags, expiry, valuelen); + // cmd could be either 'add' or 'set' + snprintf(ptr->buf, MAXBUFSIZE-1, "%s %s %d %ld %d \r\n", cmd, key, flags, expiry, valuelen); len = strlen(ptr->buf); strncat(ptr->buf, value, MAXBUFSIZE-strlen(ptr->buf)-1); @@ -240,30 +241,7 @@ } -int memcached_set(struct memcached_server *ptr, char *key, unsigned int keylen, char *value, unsigned int valuelen, unsigned int flags, unsigned long expiry){ - int len=0; - - if(memcached_connect(ptr) != MEMCACHED_SUCCESS) return MEMCACHED_FAILURE; - - snprintf(ptr->buf, MAXBUFSIZE-1, "set %s %d %ld %d \r\n", key, flags, expiry, valuelen); - len = strlen(ptr->buf); - - strncat(ptr->buf, value, MAXBUFSIZE-strlen(ptr->buf)-1); - strncat(ptr->buf, "\r\n", MAXBUFSIZE-strlen(ptr->buf)-1); - - len += valuelen + 2; - - send(ptr->fd, ptr->buf, len, 0); - - ptr->last_read_bytes = __recvtimeout(ptr->fd, ptr->buf, MAXBUFSIZE, ptr->rcv_timeout); - - if(strcmp("STORED\r\n", ptr->buf)) return MEMCACHED_FAILURE; - - return MEMCACHED_SUCCESS; -} - - -int memcached_increment(struct memcached_server *ptr, char *key, unsigned int keylen, unsigned long long value, unsigned long long *result){ +int memcached_increment(struct memcached_server *ptr, char *key, unsigned long long value, unsigned long long *result){ char *p; if(memcached_connect(ptr) != MEMCACHED_SUCCESS) return MEMCACHED_FAILURE; diff --git a/src/memc.h b/src/memc.h index 23aaaf9..37beb7f 100644 --- a/src/memc.h +++ b/src/memc.h @@ -9,9 +9,8 @@ int set_socket_options(struct memcached_server *ptr); int memcached_connect(struct memcached_server *ptr); int memcached_shutdown(struct memcached_server *ptr); -int memcached_add(struct memcached_server *ptr, char *key, unsigned int keylen, char *value, unsigned int valuelen, unsigned int flags, unsigned long expiry); -int memcached_set(struct memcached_server *ptr, char *key, unsigned int keylen, char *value, unsigned int valuelen, unsigned int flags, unsigned long expiry); -int memcached_increment(struct memcached_server *ptr, char *key, unsigned int keylen, unsigned long long value, unsigned long long *result); +int memcached_add(struct memcached_server *ptr, char *cmd, char *key, char *value, unsigned int valuelen, unsigned int flags, unsigned long expiry); +int memcached_increment(struct memcached_server *ptr, char *key, unsigned long long value, unsigned long long *result); char *memcached_get(struct memcached_server *ptr, char *key, unsigned int *len, unsigned int *flags); int memcached_mget(struct memcached_server *ptr, char *key); char *memcached_fetch_result(struct memcached_server *ptr, char *key, char *value, unsigned int *flags); diff --git a/src/piler.c b/src/piler.c index 6554d20..f334e5d 100644 --- a/src/piler.c +++ b/src/piler.c @@ -205,7 +205,7 @@ if(rc != ERR) unlink(filename); - update_counters(sdata, &counters, cfg); + update_counters(sdata, data, &counters, cfg); gettimeofday(&tv2, &tz); diff --git a/src/piler.h b/src/piler.h index b4f1791..e41e233 100644 --- a/src/piler.h +++ b/src/piler.h @@ -51,7 +51,7 @@ void check_and_create_directories(struct config *cfg, uid_t uid, gid_t gid); -void update_counters(struct session_data *sdata, struct counters *counters, struct config *cfg); +void update_counters(struct session_data *sdata, struct data *data, struct counters *counters, struct config *cfg); int retrieve_email_from_archive(struct session_data *sdata, FILE *dest, struct config *cfg); int file_from_archive_to_network(char *filename, int sd, int tls_enable, struct data *data, struct config *cfg);