// Author: Ricardas Stoma // Company: Kolmisoft // Year: 2015 // About: Script fetches radius call log from log files and saves to database #define SCRIPT_VERSION "1.11" #define SCRIPT_NAME "m2_call_log" #define RADIUS_LOG_PATH "/var/log/radius/" #define NO_PROCESS_LOCK 1 #include "m2_functions.c" // GLOBAL VARIABLES char call_id_str[256] = ""; char uniqueid[256] = ""; char uniqueid_escaped[256] = ""; char calldate[20] = ""; long long int call_id = 0; void get_m2_radius_log(); void insert_log_to_database(); int line_count(char *path); void get_call_data(); char *strmov(char *dst, char *src) { while ((*dst++ = *src++)); return dst - 1; } int main(int argc, char *argv[]) { // starting sript m2_init("Starting M2 Call Log script\n"); if (argc == 2) { strcpy(call_id_str, argv[1]); } else { m2_log("Wrong number of arguments!\n"); m2_log("Accepted arguments: [call_id]\n"); return 1; } if (!strlen(call_id_str)) { m2_log("Call id is empty\n"); return 1; } call_id = atoll(call_id_str); if (call_id <= 0) { m2_log("Call id is invalid! call_id: %lld\n", call_id); return 1; } get_call_data(); m2_log("Call id: %lld\n", call_id); m2_log("Uniqueid: %s\n", uniqueid); m2_log("Calldate: %s\n", calldate); // get radius log get_m2_radius_log(); // insert log to database insert_log_to_database(); // delete tmp files unlink("/tmp/m2_radius_log.log"); m2_log("Script completed\n"); return 0; } /* Get Radius log */ void get_m2_radius_log() { char cmd[3000] = ""; m2_log("Searching Radius log\n"); // clear old log system("echo -n > /tmp/m2_radius_log.log"); // clear old tmp file system("echo -n > /tmp/m2_radius_call_log_files"); // add default file to check system("echo '/var/log/radius/radius.log' >> /tmp/m2_radius_call_log_files"); // add one file newer than calldate sprintf(cmd, "ls -1rt `find /var/log/radius/*radius.log{.,-}* -type f -newermt '%s'` | head -n 1 | grep radius >> /tmp/m2_radius_call_log_files", calldate); m2_log("%s\n", cmd); system(cmd); if (global_debug) { m2_log("/tmp/m2_radius_call_log_files output:\n"); system("cat /tmp/m2_radius_call_log_files >> " LOG_PATH); } // add one file older than calldate sprintf(cmd, "ls -1rt `find /var/log/radius/*radius.log{.,-}* -type f ! -newermt '%s'` | tail -n 1 | grep radius >> /tmp/m2_radius_call_log_files", calldate); m2_log("%s\n", cmd); system(cmd); if (global_debug) { m2_log("/tmp/m2_radius_call_log_files output:\n"); system("cat /tmp/m2_radius_call_log_files >> " LOG_PATH); } strcpy(uniqueid_escaped, uniqueid); m2_escape_string(uniqueid_escaped, '?'); m2_escape_string(uniqueid_escaped, '*'); m2_escape_string(uniqueid_escaped, '+'); m2_escape_string(uniqueid_escaped, '.'); // get log sprintf(cmd, "/usr/bin/zgrep -a -F '%s' `cat /tmp/m2_radius_call_log_files | sed '/^$/d' | uniq` | " "sed 's| : Info: ||g' | perl -pe 's|\\[%s.*?\\] ||g' | " "sed 's| Searching for session|Searching for session|g' | " "sed 's|%s[^:]*:||g' > /tmp/m2_radius_log.log", uniqueid, uniqueid_escaped, RADIUS_LOG_PATH); m2_log("%s\n", cmd); system(cmd); if (global_debug) { m2_log("/tmp/m2_radius_log.log output:\n"); system("cat /tmp/m2_radius_log.log >> " LOG_PATH); } } /* Check if log exist and insert it to database */ void insert_log_to_database() { FILE *f; size_t fsize; char fbuffer[2048 * 1024] = ""; // file size maximum 2048 kByte char tmp[2048 * 1024] = ""; char *tmppos = NULL; char sql_start[256] = ""; char sql_end[500] = ""; int radius_log_size = 0; // read file f = fopen("/tmp/m2_radius_log.log", "r"); if (f == NULL) { m2_log("Cannot open /tmp/m2_radius_log.log\n"); exit(1); } fsize = fread(fbuffer, 1, sizeof(fbuffer), f); fclose(f); strcpy(sql_start, "UPDATE call_logs SET radius_log = '"); tmppos = strmov(tmp, sql_start); tmppos += mysql_real_escape_string(&mysql, tmppos, fbuffer, fsize); sprintf(sql_end, "' WHERE uniqueid = 'log_%s'", uniqueid); tmppos = strmov(tmppos, sql_end); *tmppos++ = (char)0; // check radius log size radius_log_size = line_count("/tmp/m2_radius_log.log"); // insert log if (radius_log_size <= 2) { m2_log("Radius log is empty\n"); } else if (radius_log_size > 300) { m2_log("Radius log is too large (%d lines). Log will not be inserted to database\n", radius_log_size); } else { m2_log("Inserting log to database\n"); if (m2_mysql_query(tmp)) { exit(1); } } } /* Check how many lines does a file contain */ int line_count(char *path) { char command[512] = ""; char pipe_buffer[64] = ""; sprintf(command, "cat %s | wc -l", path); FILE *pipe = popen(command, "r"); if (pipe == NULL) { m2_log("Command failed: %s\n", command); exit(1); } fgets(pipe_buffer, 64, pipe); pclose(pipe); m2_log("%s length: %d\n", path, atoi(pipe_buffer)); return atoi(pipe_buffer); } /* Get call data */ void get_call_data() { MYSQL_RES *result; MYSQL_ROW row; char query[2048] = ""; sprintf(query, "SELECT calldate, uniqueid FROM calls WHERE id = %lld LIMIT 1", call_id); // get calldate if (m2_mysql_query(query)) { exit(1); } result = mysql_store_result(&mysql); while ((row = mysql_fetch_row(result)) != NULL) { if (row[0]) strcpy(calldate, row[0]); if (row[1]) strcpy(uniqueid, row[1]); } mysql_free_result(result); }