<commit_before>#include "GeoIP.h"

#define GEOIP_PATH "/usr/share/GeoIP"

#define TEST_IP "8.8.8.8"
#define TEST_COUNTRY "US"

#define NAGIOS_OK 0
#define NAGIOS_ERROR 1

int print_status(char *text, int ret) {
  printf("%s\n", text);

  exit(ret);
}

int main(int argc, char **argv) {
  GeoIP *gi;
  const char *returned_country;

  gi = GeoIP_open(GEOIP_PATH "/GeoIP.dat", GEOIP_STANDARD);

  if (gi == NULL)
    print_status("CRITICAL: cannot open database", NAGIOS_ERROR); 
  
  returned_country = GeoIP_country_code_by_addr(gi, TEST_IP);

  if(returned_country == NULL)
    print_status("CRITICAL: cannot query database", NAGIOS_ERROR); 

  if(strcmp(returned_country, TEST_COUNTRY) != 0) {
    print_status("CRITICAL: country query returned different values", NAGIOS_ERROR); 
  }

  GeoIP_delete(gi);
  print_status("OK", NAGIOS_OK);

  return 0;
}<commit_msg>Add db version checking<commit_after>#include <GeoIP.h>
#include <string.h>
#include <time.h>

#define GEOIP_PATH "/usr/share/GeoIP"

#define TEST_IP "8.8.8.8"
#define TEST_COUNTRY "US"

// db version: 20150106
#define DATE_LEN 8
#define DB_AGE_WARNING 60
#define DB_AGE_ERROR 300

#define NAGIOS_OK 0
#define NAGIOS_WARNING 1
#define NAGIOS_ERROR 2

void open_db(GeoIP **gi) {
  *gi = GeoIP_open(GEOIP_PATH "/GeoIP.dat", GEOIP_STANDARD);

  if (*gi == NULL)
    print_status("CRITICAL: cannot open database", NAGIOS_ERROR);
}
  
void close_db(GeoIP **gi) {
  GeoIP_delete(*gi);
}

void check_country(GeoIP *gi) {
  const char *returned_country;
  
  returned_country = GeoIP_country_code_by_addr(gi, TEST_IP);

  if(returned_country == NULL)
    print_status("CRITICAL: cannot query database", NAGIOS_ERROR); 

  if(strcmp(returned_country, TEST_COUNTRY) != 0) {
    print_status("CRITICAL: country query returned different values", NAGIOS_ERROR); 
  }
}

void check_version(GeoIP *gi) {
  const char *info;
  char *p;
  char db_date[DATE_LEN+1];
  struct tm db_date_tm;
  time_t db_date_timet;
  time_t now;
  double seconds;
  int days;
  char message[100];

  bzero(db_date, DATE_LEN+1);
  bzero(&db_date_tm, sizeof(struct tm));

  info = GeoIP_database_info(gi);
  p = strstr(info, " ");
  strncpy(db_date, p+1, DATE_LEN);
  
  strptime(db_date, "%Y%m%d", &db_date_tm);
  db_date_timet = mktime(&db_date_tm);
  time(&now);
 
  seconds = difftime(now, db_date_timet);
  days = seconds / 3600 / 24;

  if(days > DB_AGE_ERROR) {
    sprintf(message, "ERROR: database is %d old\n", days);
    print_status(message, NAGIOS_ERROR);
  } else if (days > DB_AGE_WARNING) {
    sprintf(message, "WARNING: database is %d old\n", days);
    print_status(message, NAGIOS_WARNING);
  } 
}

int print_status(char *text, int ret) {
  printf("%s\n", text);

  exit(ret);
}

int main(int argc, char **argv) {
  GeoIP *gi;

  open_db(&gi);
  check_country(gi);
  check_version(gi);
  close_db(&gi);
  
  print_status("OK", NAGIOS_OK);

  return 0;
}
{'fraction_non_alphanumeric': 0.13581523612547397, 'fraction_numerical': 0.014822475008617718, 'mean_word_length': 4.570057581573896, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2676940', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1172, 'n_words': 255}