

#include<stdio.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<string.h>
#include<pthread.h>

#include"sitar.h"
#include"sitar_com.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"


extern global_data_t gd;

// #define SCAN_PSQL_DEBUG
// $scanpgsql -t <timeout> -s <server> -u <username> -p <passwd> -d <database> -P <port> -q <query>


// コマンド監視モード
char * scan_psql_command(char * backdata, char * query, char * server, char * username, char * passwd, char * database, int port, int timeout, int * pst, char * plai)
{
char * comdata;
char * compath = "/usr/local/zither/bin/scanpgsql";

comdata = (char *)calloc(BUF_LEN, sizeof(char));

if(timeout > 20){
   timeout = 20;
   }
if(timeout < 2){
   timeout = 2;
   }

comdata = safe_sprintf(comdata, BUF_LEN,"%s -P %d -t %d", compath, port, timeout);

if(0 < strlen(query) && 0 != self_memcmp(query, "NOT")){
   comdata = safe_strncat(comdata, " -q '", BUF_LEN);
   comdata = safe_strncat(comdata, query, BUF_LEN);
   comdata = safe_strncat(comdata, "' ", BUF_LEN);
   }
if(0 < strlen(username) && 0 != self_memcmp(username, "NOT")){
   comdata = safe_strncat(comdata, " -u ", BUF_LEN);
   comdata = safe_strncat(comdata, username, BUF_LEN);
   }
if(0 < strlen(server) && 0 != self_memcmp(server, "NOT")){
   comdata = safe_strncat(comdata, " -s ", BUF_LEN);
   comdata = safe_strncat(comdata, server, BUF_LEN);
   }
if(0 < strlen(passwd) && 0 != self_memcmp(passwd, "NOT")){
   comdata = safe_strncat(comdata, " -p ", BUF_LEN);
   comdata = safe_strncat(comdata, passwd, BUF_LEN);
   }
if(0 < strlen(database) && 0 != self_memcmp(database, "NOT")){
   comdata = safe_strncat(comdata, " -d ", BUF_LEN);
   comdata = safe_strncat(comdata, database, BUF_LEN);
   }

comdata = safe_strncat(comdata, " 2>&1", BUF_LEN);


#ifdef SCAN_PSQL_DEBUG
printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s\n", comdata);
printf("SCAN_PSQL_DEBUG: scan_psql_comand() sb.ch_plai = %s\n", plai);
#endif

// TEST モード priority 4
if(0==self_memcmp(plai, "4")){
   #ifdef SCAN_PSQL_DEBUG
   printf("SCAN_PSQL_DEBUG: scna_comand() \n");
   #endif
   backdata = execom(comdata, backdata, (timeout + 1), 1);
   if(1 < strlen(backdata)){
      #ifdef SCAN_PSQL_DEBUG
      printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s の標準出力、エラー出力（テストモード）\n", backdata);
      #endif
      }
   if(1 >= strlen(backdata)){
      #ifdef SCAN_PSQL_DEBUG
      printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s は正常終了（テストモード）\n", comdata);
      #endif
      backdata = safe_memcpy(backdata, COM_3, BUF_MAX );
      }
   }

// ログ＆監視モード priority 11,12,13
else if(0==self_memcmp(plai, "11")
     || 0==self_memcmp(plai, "12") || 0==self_memcmp(plai, "13")){
   #ifdef SCAN_PSQL_DEBUG
   printf("SCAN_PSQL_DEBUG: scna_comand() Priority 11,12,13\n");
   #endif
   backdata = execom(comdata, backdata, (timeout + 1), 1);
   if(1 < strlen(backdata)){
      #ifdef SCAN_PSQL_DEBUG
      printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s 標準出力、エラー出力（ログ監視モード）\n", backdata);
      #endif
      if(0 == memcmp(backdata, "NG:", 3)){
         if(0==self_memcmp(plai, "11")){
            plai = safe_memcpy(plai, "1", BUF_MAX);
            }
         else if(0==self_memcmp(plai, "12")){
            plai = safe_memcpy(plai, "2", BUF_MAX);
            }
         else if(0==self_memcmp(plai, "13")){
            plai = safe_memcpy(plai, "3", BUF_MAX);
            }
         else{
            }
         #ifdef SCAN_PSQL_DEBUG
         printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s 出力にNG:を含むのでエラー priority 11,12,13 priority %d\n", backdata, plai);
         #endif
         }
      else{
         #ifdef SCAN_PSQL_DEBUG
         printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s 出力にNG:を含まない priority 11,12,13\n", backdata);
         #endif
         }
      }
   if(1 >= strlen(backdata)){
      #ifdef SCAN_PSQL_DEBUG
      printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s \n", comdata);
      #endif
      backdata = safe_memcpy(backdata, COM_3, BUF_MAX );
      }
   }

// 通常コマンド監視モードpriority 1,2,3
else{
   #ifdef SCAN_PSQL_DEBUG
   printf("SCAN_PSQL_DEBUG: scan_psql_comand() 通常のコマンド監視モードPriority 1,2,3\n");
   #endif
   backdata = execom(comdata, backdata, (timeout + 1), 1);
   if(1 < strlen(backdata)){
      #ifdef SCAN_PSQL_DEBUG
      printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s 標準出力、エラー出力\n", backdata);
      #endif
      }
   if(1 >= strlen(backdata)){
      #ifdef SCAN_PSQL_DEBUG
      printf("SCAN_PSQL_DEBUG: scan_psql_comand() %s コマンド監視正常終了\n", comdata);
      #endif
      }
   }

#ifdef SCAN_PSQL_DEBUG
printf("SCAN_PSQL_DEBUG: scan_psql_comand(): コマンド出力 ---> %s -->カウント %d\n", backdata, (int)strlen(backdata));
#endif

swap_mem(backdata, '\n', ' ');
swap_mem(backdata, '\r', ' ');
swap_mem(backdata, ',', ' ');

if(2 > (int)strlen(backdata)){  // 改行分をいれる
   * pst = 0;
   #ifdef SCAN_PSQL_DEBUG
   printf("SCAN_PSQL_DEBUG: scan_psql_comand(): 正常終了\n");
   #endif
   }
else{
   * pst = -1;
   #ifdef SCAN_PSQL_DEBUG
   printf("SCAN_PSQL_DEBUG: scan_psql_comand(): エラー出力\n");
   #endif
   }

free(comdata);
return backdata;
}




