#!/bin/bash

namespace=$1

kubectl -n ${namespace} wait --for=condition=ready pod/elasticsearch-1 --timeout=600s

export ES_NODEPORT=`kubectl -n ${namespace} get svc elasticsearch -o jsonpath='{.spec.ports[*].nodePort}'`

export MASTER_IP=`kubectl get node -o wide | grep control-plane | awk '{print $6}'`
echo $MASTER_IP

export NUM_SHARDS=2
export NUM_REPLICAS=1

SECURE=true

if [ $SECURE = true ]
then
PARAM="-u elastic:elastic --insecure"
PROTO="https"
else
PARAM=""
PROTO="http"
fi

echo Secure=$SECURE
echo Param=$PARAM
echo Proto=$PROTO

curl ${PARAM} -X GET ${PROTO}://${MASTER_IP}:${ES_NODEPORT}/_cat/indices

echo "curl ${PARAM} -X GET ${PROTO}://${MASTER_IP}:${ES_NODEPORT}/_cat/indices"

# kubernetes_cluster_info
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_cluster_info' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_cluster_info"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "sort.field": "mtime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "date": {
        "type": "long"
      },
      "mtime": {
        "type": "long"
      },
      "nodes": {
        "type": "text",
        "index": false
      }
    }
  }
}'

# kubernetes_cluster_history
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_cluster_history' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_cluster_history' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_cluster_history-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "kubernetes_cluster_history"
      },
      "sort.field": "mtime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "mtime": {
        "type": "long"
      },
      "nodes": {
        "type": "text",
        "index": false
      }
    }
  },
  "aliases": {
    "kubernetes_cluster_history": {}
  }
}'

# kubernetes_info
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_info' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_info' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_info-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "kubernetes_info"
      },
      "sort.field": "mtime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "mtime": {
        "type": "long"
      },
      "data": {
        "type": "text",
        "index": false
      }
    }
  },
  "aliases": {
    "kubernetes_info": {}
  }
}'



# kubernetes_event_info
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_event_info' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_event_info' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_event_info-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "kubernetes_event_info"
      }
    },
    "analysis": {
      "analyzer": {
        "my_customer_ngram_analyzer": {
          "tokenizer": "my_customer_ngram_tokenizer"
        }
      },
      "tokenizer": {
        "my_customer_ngram_tokenizer": {
          "type": "ngram",
          "min_gram": "2",
          "max_gram": "3"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "type": {
        "type": "keyword"
      },
      "unixtime": {
        "type": "long"
      },
      "kind": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "firsttime": {
        "type": "long"
      },
      "lasttime": {
        "type": "long"
      },
      "data": {
        "type": "text",
        "index": false
      },
      "id": {
        "type": "keyword"
      },
      "reason": {
        "type": "keyword"
      },
      "message": {
        "type": "text",
        "fields": {
                "ngram": {
                  "type": "text",
                  "analyzer": "my_customer_ngram_analyzer"
                }
        }
      },
      "count": {
        "type": "integer"
      },
      "sourceComponent": {
        "type": "keyword"
      },
      "sourceHost": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "kubernetes_event_info": {}
  }
}'




# kubernetes_job_info
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_job_info' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_job_info' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_job_info-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "kubernetes_job_info"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "starttime": {
        "type": "long"
      },
      "endtime": {
        "type": "long"
      },
      "duration": {
        "type": "long"
      },
      "commandlist": {
        "type": "text",
        "index": false
      },
      "labellist": {
        "type": "text",
        "index": false
      },
      "active": {
        "type": "boolean"
      },
      "status": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "kubernetes_job_info": {}
  }
}'



# kubernetes_cronjob_info
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_cronjob_info' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_cronjob_info' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_cronjob_info-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "kubernetes_cronjob_info"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "jobname": {
        "type": "keyword"
      },
      "kind": {
        "type": "keyword"
      },
      "starttime": {
        "type": "long"
      },
      "endtime": {
        "type": "long"
      },
      "duration": {
        "type": "long"
      },
      "lastruntime": {
        "type": "long"
      },
      "arguments": {
        "type": "text",
        "index": false
      },
      "schedule": {
        "type": "keyword"
      },
      "active": {
        "type": "boolean"
      },
      "status": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "kubernetes_cronjob_info": {}
  }
}'




# kubernetes_network_connectivity
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_network_connectivity' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_network_connectivity' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_network_connectivity-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "kubernetes_network_connectivity"
      }
    }
  },
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "long"
      },
      "cluster": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "container": {
        "type": "keyword"
      },
      "pid": {
        "type": "integer"
      },
      "peerNode": {
        "type": "keyword"
      },
      "peerNamespace": {
        "type": "keyword"
      },
      "peerService": {
        "type": "keyword"
      },
      "peerPod": {
        "type": "keyword"
      },
      "peerContainer": {
        "type": "keyword"
      },
      "peerPid": {
        "type": "integer"
      }
    }
  },
  "aliases": {
    "kubernetes_network_connectivity": {}
  }
}'



# sparse_log
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/sparse_log' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sparse_log' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sparse_log-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "sparse_log"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "date": {
        "type": "keyword"
      },
      "targetType": {
        "type": "keyword"
      },
      "targetId": {
        "type": "keyword"
      },
      "unixtime": {
        "type": "long"
      },
      "logpath": {
        "type": "text",
        "index": false
      },
      "contents": {
        "type": "text"
      },
      "lineNumber": {
        "type": "integer"
      },
      "probability": {
        "type": "float"
      },
      "subentityId": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "sparse_log": {}
  }
}'



# sparse_model
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sparse_model' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sparse_model"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s"
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "targetType": {
        "type": "keyword"
      },
      "targetId": {
        "type": "keyword"
      },
      "modifiedDate": {
        "type": "long"
      },
      "logPath": {
        "type": "keyword"
      },
      "savedModel": {
        "type": "text",
        "index": false
      }
    }
  }
}'



# kubernetes_pod_info
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_pod_info' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_pod_info' -H 'Content-Type: application/json' -d '{
"order": 0,
  "index_patterns": [
    "kubernetes_pod_info-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "kubernetes_pod_info"
      }
    }
  },
  "mappings": {
    "properties": {
      "eventType": {"type": "keyword"},
      "cluster": {"type": "keyword"},
      "namespace": {"type": "keyword"},
      "node": {"type": "keyword"},
      "pod": {"type": "keyword"},
      "podUID": {"type": "keyword"},
      "podCreationTimestamp": {"type": "long"},
      "podDeletionTimestamp": {"type": "long"},
      "podDeletionGracePeriod": {"type": "long"},
      "resourceVersion": {"type": "keyword"},
      "ownerKind": {"type": "keyword"},
      "ownerName": {"type": "keyword"},
      "ownerUID": {"type": "keyword"},
      "podPhase": {"type": "keyword"},
      "podIP": {"type": "keyword"},
      "podStartTime": {"type": "long"},
      "podReady": {"type": "boolean"},
      "podContainersReady": {"type": "boolean"},
      "isInitContainer": {"type": "boolean"},
      "containerName": {"type": "keyword"},
      "containerID": {"type": "keyword"},
      "containerImage": {"type": "keyword"},
      "containerImageShort": {"type": "keyword"},
      "containerReady": {"type": "boolean"},
      "containerRestartCount": {"type": "integer"},
      "containerState": {"type": "keyword"},
      "containerStartTime": {"type": "long"},
      "containerMessage": {"type": "keyword"},
      "containerReason": {"type": "keyword"},
      "containerFinishTime": {"type": "long"},
      "containerExitCode": {"type": "integer"},
      "containerLastState": {"type": "keyword"},
      "containerLastStartTime": {"type": "long"},
      "containerLastMessage": {"type": "keyword"},
      "containerLastReason": {"type": "keyword"},
      "containerLastFinishTime": {"type": "long"},
      "containerLastExitCode": {"type": "integer"}
    }
  },
  "aliases": {
    "kubernetes_pod_info": {}
  }
}'



# kubernetes_pod_history
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_pod_history' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_pod_history' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_pod_history-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "kubernetes_pod_history"
      }
    }
  },
  "mappings": {
    "properties": {
      "deployName": {
        "type": "keyword"
      },
      "deployType": {
        "type": "keyword"
      },
      "deployDate": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "nodeId": {
        "type": "keyword"
      },
      "podId": {
        "type": "keyword"
      },
      "podPhase": {
        "type": "keyword"
      },
      "startTime": {
        "type": "keyword"
      },
      "endTime": {
        "type": "keyword"
      },
      "exitCode": {
        "type": "integer"
      },
      "reason": {
        "type": "keyword"
      },
      "message": {
        "type": "text"
      },
      "time": {
        "type": "long"
      },
      "containerId": {
        "type": "keyword"
      },
      "containerName": {
        "type": "keyword"
      },
      "containerPhase": {
        "type": "keyword"
      },
      "eventAction": {
        "type": "keyword"
      },
      "containerStartTime": {
        "type": "keyword"
      },
      "containerEndTime": {
        "type": "keyword"
      },
      "containerImage": {
        "type": "keyword"
      },
      "containerImageShort": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "kubernetes_pod_history": {}
  }
}'




# metric_score
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/metric_score' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/metric_score' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "metric_score-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "metric_score"
      },
      "sort.field": "unixtime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "anomaly": {
        "type": "boolean"
      },
      "clstId": {
        "type": "keyword"
      },
      "contName": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "podId": {
        "type": "keyword"
      },
      "instance": {
        "type": "keyword"
      },
      "entityId": {
        "type": "keyword"
      },
      "entityType": {
        "type": "keyword"
      },
      "metricId": {
        "type": "keyword"
      },
      "nodeId": {
        "type": "keyword"
      },
      "score": {
        "type": "integer"
      },
      "subKey": {
        "type": "keyword"
      },
      "unixtime": {
        "type": "long"
      },
      "yhatLowerUpper": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  },
  "aliases": {
    "metric_score": {}
  }
}'




# entity_score
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/entity_score' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/entity_score' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "entity_score-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "entity_score"
      },
      "sort.field": "unixtime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "clstId": {
        "type": "keyword"
      },
      "contName": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "podId": {
        "type": "keyword"
      },
      "entityId": {
        "type": "keyword"
      },
      "entityType": {
        "type": "keyword"
      },
      "unixtime": {
        "type": "long"
      },
      "nodeId": {
        "type": "keyword"
      },
      "maxId": {
        "type": "keyword"
      },
      "maxScore": {
        "type": "integer"
      },
      "entityScore": {
        "type": "integer"
      }
    }
  },
  "aliases": {
    "entity_score": {}
  }
}'


# timeline_score
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/timeline_score' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/timeline_score' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "timeline_score-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "timeline_score"
      },
      "sort.field": "unixtime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "clstId": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "entityType": {
        "type": "keyword"
      },
      "criticalCount": {
        "type": "integer"
      },
      "warningCount": {
        "type": "integer"
      },
      "attentionCount": {
        "type": "integer"
      },
      "normalCount": {
        "type": "integer"
      },
      "unixtime": {
        "type": "long"
      }
    }
  },
  "aliases": {
    "timeline_score": {}
  }
}'



# spaninfo
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/spaninfo' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/spaninfo' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "spaninfo-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "0",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "spaninfo"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "version": {
        "type": "keyword"
      },
      "ip": {
        "type": "keyword"
      },
      "traceId": {
        "type": "keyword"
      },
      "spanId": {
        "type": "keyword"
      },
      "parentSpanId": {
        "type": "keyword"
      },
      "protocolType": {
        "type": "keyword"
      },
      "startTime": {
        "type": "long"
      },
      "duration": {
        "type": "long"
      },
      "endTime": {
        "type": "long"
      },
      "operation": {
        "type": "keyword"
      },
      "spanKind": {
        "type": "keyword"
      },
      "component": {
        "type": "keyword"
      },
      "error": {
        "type": "boolean"
      },
      "peerAddress": {
        "type": "keyword"
      },
      "peerHostname": {
        "type": "keyword"
      },
      "peerIpv4": {
        "type": "keyword"
      },
      "peerIpv6": {
        "type": "keyword"
      },
      "peerPort": {
        "type": "integer"
      },
      "peerService": {
        "type": "keyword"
      },
      "samplingPriority": {
        "type": "keyword"
      },
      "httpStatusCode": {
        "type": "integer"
      },
      "httpUrl": {
        "type": "keyword"
      },
      "httpMethod": {
        "type": "keyword"
      },
      "httpApi": {
        "type": "keyword"
      },
      "dbInstance": {
        "type": "keyword"
      },
      "dbStatement": {
        "type": "keyword"
      },
      "dbType": {
        "type": "keyword"
      },
      "dbUser": {
        "type": "keyword"
      },
      "messagebusDestination": {
        "type": "keyword"
      },
      "logs": {
        "dynamic": false,
        "type": "nested",
        "properties": {
          "fields": {
            "dynamic": false,
            "type": "nested",
            "properties": {
              "value": {
                "ignore_above": 256,
                "type": "keyword"
              },
              "key": {
                "type": "keyword"
              }
            }
          },
          "timestamp": {
            "type": "long"
          }
        }
      }
    }
  },
  "aliases": {
    "spaninfo": {}
  }
}'



# sta_podinfo
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/sta_podinfo' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sta_podinfo' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sta_podinfo-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "0",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "sta_podinfo"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "timestamp": {
        "type": "long"
      },
      "version": {
        "type": "keyword"
      },
      "components": {
        "type": "keyword",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  },
  "aliases": {
    "sta_podinfo": {}
  }
}'


# sta_httpapi
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/sta_httpapi' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sta_httpapi' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sta_httpapi-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "0",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "sta_httpapi"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "timestamp": {
        "type": "long"
      },
      "api": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "sta_httpapi": {}
  }
}'



# sta_httpsummary
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/sta_httpsummary' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sta_httpsummary' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sta_httpsummary-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "0",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "sta_httpsummary"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "timestamp": {
        "type": "long"
      },
      "pod": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "api": {
        "type": "keyword"
      },
      "countTotal": {
        "type": "integer"
      },
      "errorCountTotal": {
        "type": "integer"
      },
      "timeTotalMicrosec": {
        "type": "integer"
      },
      "methods": {
        "type": "keyword",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "statuses": {
        "type": "integer",
        "fields": {
          "integer": {
            "type": "integer"
          }
        }
      }
    }
  },
  "aliases": {
    "sta_httpsummary": {}
  }
}'



# sta_relation
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/sta_relation' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sta_relation' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sta_relation-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "0",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "sta_relation"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "timestamp": {
        "type": "long"
      },
      "parent": {
        "type": "keyword"
      },
      "children": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "count": {
            "type": "integer"
          }
        }
      }
    }
  },
  "aliases": {
    "sta_relation": {}
  }
}'



# sta_externalrelation
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/sta_externalrelation' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sta_externalrelation' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sta_externalrelation-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "0",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "sta_externalrelation"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "timestamp": {
        "type": "long"
      },
      "externalNamespace": {
        "type": "keyword"
      },
      "externalService": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "sta_externalrelation": {}
  }
}'



# sta_traceinfo
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/sta_traceinfo' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sta_traceinfo' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sta_traceinfo-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "0",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "sta_traceinfo"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "traceId": {
        "type": "keyword"
      },
      "serviceName": {
        "type": "keyword"
      },
      "operationName": {
        "type": "keyword"
      },
      "spanSize": {
        "type": "integer"
      },
      "relatedServices": {
        "type": "keyword",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "startTime": {
        "type": "long"
      },
      "endTime": {
        "type": "long"
      },
      "duration": {
        "type": "long"
      },
      "error": {
        "type": "boolean"
      }
    }
  },
  "aliases": {
    "sta_traceinfo": {}
  }
}'



# sta_tracetrend
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/sta_tracetrend' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/sta_tracetrend' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "sta_tracetrend-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "0",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "sta_tracetrend"
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "serviceName": {
        "type": "keyword"
      },
      "endTimeGTE": {
        "type": "long"
      },
      "endTimeLT": {
        "type": "long"
      }
    },
    "dynamic_templates": [
      {
        "totals": {
          "match": "total*",
          "mapping": {"type": "integer"}
        }
      },
      {
        "errors": {
          "match": "error*",
          "mapping": {"type": "integer"}
        }
      }
    ]
  },
  "aliases": {
    "sta_tracetrend": {}
  }
}'

# script_history
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/script_history' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'



curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/script_history' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "script_history-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "script_history"
      }
    }
  },
  "mappings": {
    "properties": {
     "taskId": {
        "type": "long"
      },
      "scriptName": {
        "type": "keyword"
      },
      "agentName": {
        "type": "keyword"
      },
      "targetFile": {
        "type": "keyword"
      },
      "args": {
        "type": "keyword",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "validCmd": {
        "type": "keyword"
      },
      "validVal": {
        "type": "keyword"
      },
      "valid": {
        "type": "boolean"
      },
      "validResult": {
        "type": "keyword"
      },
      "cronExp": {
        "type": "keyword"
      },
      "createUser": {
        "type": "keyword"
      },
      "startTime": {
        "type": "long"
      },
      "endTime": {
        "type": "long"
      },
      "error": {
        "type": "boolean"
      },
      "result": {
        "type": "keyword"
      },
      "order": {
        "type": "keyword"
      },
      "mtime": {
        "type": "keyword"
      }
    }
   },
  "aliases": {
    "script_history": {}
  }
}'


# kubernetes_audit_log
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/kubernetes_audit_log' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/kubernetes_audit_log' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "kubernetes_audit_log-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": "1",
      "refresh_interval": "1s",
	  "lifecycle": {
        "name": "kubernetes_audit_log"
      },
      "sort.field": "stageTimestamp",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
	  "verb": {
        "type": "keyword"
      },
	  "userName": {
        "type": "keyword"
      },
	  "sourceIps": {
        "type": "keyword"
      },
	  "resource": {
        "type": "keyword"
      },
	  "code": {
        "type": "keyword"
      },
	  "requestReceivedTimestamp": {
        "type": "long"
      },
      "stageTimestamp": {
        "type": "long"
      },
	  "durationTimestamp": {
        "type": "long"
      },
    "data": {
        "type": "text",
        "index": false
      }
    }
  },
  "aliases": {
    "kubernetes_audit_log": {}
  }
}'

# license_history
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/license_history' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "90d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/license_history' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "license_history-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": "2",
      "number_of_replicas": "1",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "license_history"
      },
      "sort.field": "checkTime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "licenseType": {
        "type": "integer"
      },
      "expireDate": {
        "type": "text"
      },
      "targetNodesCount": {
        "type": "integer"
      },
      "realNodesCount": {
        "type": "integer"
      },
      "targetPodsCount": {
        "type": "integer"
      },
      "realPodsCount": {
        "type": "integer"
      },
      "targetSvcsCount": {
        "type": "integer"
      },
      "realSvcsCount": {
        "type": "integer"
      },
      "targetCoreCount": {
        "type": "integer"
      },
      "realCoreCount": {
        "type": "integer"
      },
      "allowableRange": {
        "type": "integer"
      },
      "licenseClusterId": {
        "type": "keyword"
      },
      "tenantId": {
        "type": "keyword"
      },
      "checkTime": {
        "type": "date",
        "format": "epoch_millis"
      },
      "checkResult": {
        "type": "integer"
      }
    }
  },
  "aliases": {
    "license_history": {}
  }
}'

# alert_event_history
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/alert_event_history' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/alert_event_history' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "alert_event_history-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "alert_event_history"
      }
    }
  },
  "mappings": {
    "properties": {
      "alertName": {
        "type": "keyword"
      },
      "clusterId": {
        "type": "keyword"
      },
	  "data": {
        "type": "text",
		"index": false
      },
	  "entityId": {
        "type": "keyword"
      },
	  "entityType": {
        "type": "keyword"
      },
	  "level": {
        "type": "keyword"
      },
	  "metaId": {
        "type": "keyword"
      },
	  "namespace": {
        "type": "keyword"
      },
	  "startsAt": {
        "type": "long"
      },
	  "threshold": {
        "type": "double"
      },
	  "value": {
        "type": "double"
      },
	  "message": {
        "type": "keyword"
      },
	  "endsAt": {
        "type": "long"
      },
	  "status": {
        "type": "keyword"
      },
	  "hookCollectAt": {
        "type": "long"
      }
    }
  },
  "aliases": {
    "alert_event_history": {}
  }
}'

# JSPD ilm
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/jspd_ilm' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "1d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

# jspd_lite-activetxn
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-activetxn' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "jspd_lite-activetxn-*"
    ],
    "settings": {
        "index": {
	    "number_of_shards": '""${NUM_SHARDS}""',
	    "number_of_replicas": '""${NUM_REPLICAS}""',
            "refresh_interval": "1s",
            "lifecycle": {
                "name": "jspd_ilm"
            }
        }
    },
    "mappings": {
        "properties": {
            "server_uuid": {
                "type": "keyword"
            },
            "time": {
                "type": "long"
            },
            "cluster": {
                "type": "keyword"
            },
            "namespace": {
                "type": "keyword"
            },
            "node": {
                "type": "keyword"
            },
            "service": {
                "type": "keyword"
            },
            "pod": {
                "type": "keyword"
            },
            "start_time": {
                "type": "long"
            },
            "tid": {
                "type": "keyword"
            },
            "txn_name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "cpu_time": {
                "type": "integer"
            },
            "memory_usage": {
                "type": "integer"
            },
            "web_id": {
                "type": "integer"
            },
            "prepare_count": {
                "type": "integer"
            },
            "sql_exec_count": {
                "type": "integer"
            },
            "fetch_count": {
                "type": "integer"
            },
            "active_sql_elapse_time": {
                "type": "integer"
            },
            "db_id": {
                "type": "integer"
            },
            "sql_text": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 32766,
                        "type": "keyword"
                    }
                }
            },
            "thread_id": {
                "type": "long"
            },
            "state": {
                "type": "short"
            },
            "method_id": {
                "type": "integer"
            },
            "method_seq": {
                "type": "integer"
            },
            "stack_crc": {
                "type": "integer"
            },
            "thread_memory_usage": {
                "type": "integer"
            },
            "http_method": {
                "type": "keyword"
            }
        }
    },
    "aliases": {
        "jspd_lite-activetxn": {}
    }
}'

# jspd_lite-alert
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-alert' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "jspd_lite-alert-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "jspd_ilm"
      }
    }
  },
  "mappings": {
    "properties": {
      "server_uuid": {
        "type": "keyword"
      },
      "time": {
        "type": "long"
      },
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "status": {
        "type": "short"
      },
      "value": {
        "type": "integer"
      },
      "pid": {
        "type": "integer"
      }
    }
  },
  "aliases": {
    "jspd_lite-alert": {}
  }
}'

# jspd_lite-e2einfo
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-e2einfo' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "jspd_lite-e2einfo-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "jspd_ilm"
      }
    }
  },
  "mappings": {
    "properties": {
      "server_uuid": {
        "type": "keyword"
      },
      "time": {
        "type": "long"
      },
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "root_tid": {
        "type": "keyword"
      },
      "tid": {
        "type": "keyword"
      },
      "e2e_info_type": {
        "type": "short"
      },
      "e2e_key": {
        "type": "keyword"
      },
      "elapse_time": {
        "type": "integer"
      },
      "dest_url": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "jspd_lite-e2einfo": {}
  }
}'

# jspd_lite-methodname
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-methodname' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "jspd_lite-methodname-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "jspd_ilm"
      }
    }
  },
  "mappings": {
    "properties": {
      "server_uuid": {
        "type": "keyword"
      },
      "time": {
        "type": "long"
      },
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "method_id": {
        "type": "integer"
      },
      "class_name": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        }
      },
      "method_name": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        }
      }
    }
  },
  "aliases": {
    "jspd_lite-methodname": {}
  }
}'

# jspd_lite-sqldbinfo
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-sqldbinfo' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "jspd_lite-sqldbinfo-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "jspd_ilm"
      }
    }
  },
  "mappings": {
    "properties": {
      "server_uuid": {
        "type": "keyword"
      },
      "time": {
        "type": "long"
      },
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "db_id": {
        "type": "integer"
      },
      "url": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "jspd_lite-sqldbinfo": {}
  }
}'

# jspd_lite-txninfo
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-txninfo' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "jspd_lite-txninfo-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "jspd_ilm"
      }
    }
  },
  "mappings": {
    "properties": {
      "server_uuid": {
        "type": "keyword"
      },
      "time": {
        "type": "long"
      },
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "start_time": {
        "type": "long"
      },
      "end_time": {
        "type": "long"
      },
      "tid": {
        "type": "keyword"
      },
      "txn_name": {
        "type": "keyword"
      },
      "client_ip": {
        "type": "keyword"
      },
      "exception": {
        "type": "short"
      },
      "thread_cpu_time": {
        "type": "integer"
      },
      "thread_memory_usage": {
        "type": "integer"
      },
      "web_id": {
        "type": "integer"
      },
      "open_conn": {
        "type": "integer"
      },
      "close_conn": {
        "type": "integer"
      },
      "open_stmt": {
        "type": "integer"
      },
      "close_stmt": {
        "type": "integer"
      },
      "open_rs": {
        "type": "integer"
      },
      "close_rs": {
        "type": "integer"
      },
      "prepare_count": {
        "type": "integer"
      },
      "sql_execute_count": {
        "type": "integer"
      },
      "sql_elapse_time": {
        "type": "integer"
      },
      "sql_elapse_max": {
        "type": "integer"
      },
      "fetch_count": {
        "type": "integer"
      },
      "fetch_time": {
        "type": "integer"
      },
      "internal_fetch_count": {
        "type": "integer"
      },
      "txn_flag": {
        "type": "integer"
      },
      "http_method": {
        "type": "keyword"
      },
      "http_status": {
        "type": "integer"
      },
      "duration": {
        "type": "long"
      }
    }
  },
  "aliases": {
    "jspd_lite-txninfo": {}
  }
}'

# jspd_lite-txnmethod
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-txnmethod' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "jspd_lite-txnmethod-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "jspd_ilm"
      }
    }
  },
  "mappings": {
    "properties": {
      "server_uuid": {
        "type": "keyword"
      },
      "time": {
        "type": "long"
      },
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "tid": {
        "type": "keyword"
      },
      "method_seq": {
        "type": "integer"
      },
      "method_id": {
        "type": "integer"
      },
      "calling_method_id": {
        "type": "integer"
      },
      "stack_crc32": {
        "type": "integer"
      },
      "calling_stack_crc32": {
        "type": "integer"
      },
      "elapse_time": {
        "type": "integer"
      },
      "exec_count": {
        "type": "integer"
      },
      "error_count": {
        "type": "integer"
      },
      "cpu_time": {
        "type": "integer"
      },
      "memory": {
        "type": "integer"
      },
      "start_time": {
        "type": "long"
      },
      "method_depth": {
        "type": "integer"
      },
      "exception": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 32768,
            "type": "keyword"
          }
        }
      }
    }
  },
  "aliases": {
    "jspd_lite-txnmethod": {}
  }
}'

# jspd_lite-txnsql
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-txnsql' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "jspd_lite-txnsql-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "jspd_ilm"
      }
    }
  },
  "mappings": {
    "properties": {
      "server_uuid": {
        "type": "keyword"
      },
      "time": {
        "type": "long"
      },
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "node": {
        "type": "keyword"
      },
      "service": {
        "type": "keyword"
      },
      "pod": {
        "type": "keyword"
      },
      "tid": {
        "type": "keyword"
      },
      "db_id": {
        "type": "integer"
      },
      "cursor_id": {
        "type": "integer"
      },
      "sql_text": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 32766,
            "type": "keyword"
          }
        }
      },
      "method_id": {
        "type": "integer"
      },
      "execute_count": {
        "type": "integer"
      },
      "elapsed_time": {
        "type": "integer"
      },
      "elapsed_time_max": {
        "type": "integer"
      },
      "fetch_count": {
        "type": "integer"
      },
      "fetch_time": {
        "type": "integer"
      },
      "fetch_time_max": {
        "type": "integer"
      },
      "internal_fetch_count": {
        "type": "integer"
      }
    }
  },
  "aliases": {
    "jspd_lite-txnsql": {}
  }
}'

# jspd_lite-wasstat
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_lite-wasstat' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "jspd_lite-wasstat-*"
    ],
    "settings": {
        "index": {
            "number_of_shards": '""${NUM_SHARDS}""',
            "number_of_replicas": '""${NUM_REPLICAS}""',
            "refresh_interval": "1s",
            "lifecycle": {
                "name": "jspd_ilm"
            }
        }
    },
    "mappings": {
        "properties": {
            "server_uuid": {
                "type": "keyword"
            },
            "time": {
                "type": "long"
            },
            "cluster": {
                "type": "keyword"
            },
            "namespace": {
                "type": "keyword"
            },
            "node": {
                "type": "keyword"
            },
            "service": {
                "type": "keyword"
            },
            "pod": {
                "type": "keyword"
            },
            "active_txns": {
                "type": "integer"
            },
            "sql_exec_count": {
                "type": "long"
            },
            "sql_prepare_count": {
                "type": "long"
            },
            "sql_fetch_count": {
                "type": "long"
            },
            "txn_end_count": {
                "type": "long"
            },
            "open_file_count": {
                "type": "integer"
            },
            "close_file_count": {
                "type": "integer"
            },
            "open_socket_count": {
                "type": "integer"
            },
            "close_socket_count": {
                "type": "integer"
            },
            "txn_elapse": {
                "type": "long"
            },
            "sql_elapse": {
                "type": "long"
            },
            "txn_elapse_max": {
                "type": "long"
            },
            "sql_elapse_max": {
                "type": "long"
            },
            "txn_error_count": {
                "type": "integer"
            }
        }
    },
    "aliases": {
        "jspd_lite-wasstat": {}
    }
}'

# jspd_tta-externalrelation
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_tta-externalrelation' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "jspd_tta-externalrelation-*"
    ],
    "settings": {
        "index": {
            "number_of_shards": '""${NUM_SHARDS}""',
            "number_of_replicas": '""${NUM_REPLICAS}""',
            "refresh_interval": "1s",
            "lifecycle": {
                "name": "jspd_ilm"
            }
        }
    },
    "mappings": {
        "properties": {
            "cluster": {
                "type": "keyword"
            },
            "namespace": {
                "type": "keyword"
            },
            "time": {
                "type": "long"
            },
            "external_namespace": {
                "type": "keyword"
            },
            "external_service": {
                "type": "keyword"
            }
        }
    },
    "aliases": {
        "jspd_tta-externalrelation": {}
    }
}'

# jspd_tta-relation
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_tta-relation' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "jspd_tta-relation-*"
    ],
    "settings": {
        "index": {
            "number_of_shards": '""${NUM_SHARDS}""',
            "number_of_replicas": '""${NUM_REPLICAS}""',
            "refresh_interval": "1s",
            "lifecycle": {
                "name": "jspd_ilm"
            }
        }
    },
    "mappings": {
        "properties": {
            "cluster": {
                "type": "keyword"
            },
            "namespace": {
                "type": "keyword"
            },
            "time": {
                "type": "long"
            },
            "from_service": {
                "type": "keyword"
            },
            "to_service": {
                "type": "keyword"
            },
            "count": {
                "type": "integer"
            }
        }
    },
    "aliases": {
        "jspd_tta-relation": {}
    }
}'

# jspd_tta-txnlist
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_tta-txnlist' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "jspd_tta-txnlist-*"
    ],
    "settings": {
        "index": {
            "number_of_shards": '""${NUM_SHARDS}""',
            "number_of_replicas": '""${NUM_REPLICAS}""',
            "refresh_interval": "1s",
            "lifecycle": {
                "name": "jspd_ilm"
            }
        }
    },
    "mappings": {
        "properties": {
            "cluster": {
                "type": "keyword"
            },
            "namespace": {
                "type": "keyword"
            },
            "service": {
                "type": "keyword"
            },
            "pod": {
                "type": "keyword"
            },
            "time": {
                "type": "long"
            },
            "txn_name": {
                "type": "keyword"
            }
        }
    },
    "aliases": {
        "jspd_tta-txnlist": {}
    }
}'

# jspd_tta-txnsummary
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_tta-txnsummary' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "jspd_tta-txnsummary-*"
    ],
    "settings": {
        "index": {
            "number_of_shards": '""${NUM_SHARDS}""',
            "number_of_replicas": '""${NUM_REPLICAS}""',
            "refresh_interval": "1s",
            "lifecycle": {
                "name": "jspd_ilm"
            }
        }
    },
    "mappings": {
        "properties": {
            "cluster": {
                "type": "keyword"
            },
            "namespace": {
                "type": "keyword"
            },
            "service": {
                "type": "keyword"
            },
            "pod": {
                "type": "keyword"
            },
            "time": {
                "type": "long"
            },
            "txn_name": {
                "type": "keyword"
            },
            "req_count": {
                "type": "integer"
            },
            "resp_count": {
                "type": "integer"
            },
            "total_duration": {
                "type": "long"
            },
            "failed": {
                "type": "integer"
            },
            "http_methods": {
                "type": "keyword",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    }
                }
            },
            "http_statuses": {
                "type": "integer",
                "fields": {
                    "integer": {
                        "type": "integer"
                    }
                }
            }
        }
    },
    "aliases": {
        "jspd_tta-txnsummary": {}
    }
}'

# jspd_tta-txntrend
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/jspd_tta-txntrend' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "jspd_tta-txntrend-*"
    ],
    "settings": {
        "index": {
            "number_of_shards": '""${NUM_SHARDS}""',
            "number_of_replicas": '""${NUM_REPLICAS}""',
            "refresh_interval": "1s",
            "lifecycle": {
                "name": "jspd_ilm"
            }
        }
    },
    "mappings": {
        "properties": {
            "server_uuid": {
                "type": "keyword"
            },
            "time": {
                "type": "long"
            },
            "cluster": {
                "type": "keyword"
            },
            "namespace": {
                "type": "keyword"
            },
            "service": {
                "type": "keyword"
            },
            "pod": {
                "type": "keyword"
            },
            "endTimeGTE": {
                "type": "long"
            },
            "endTimeLT": {
                "type": "long"
            }
        },
        "dynamic_templates": [
            {
                "totals": {
                    "match": "total*",
                    "mapping": {
                        "type": "integer"
                    }
                }
            },
            {
                "errors": {
                    "match": "error*",
                    "mapping": {
                        "type": "integer"
                    }
                }
            }
        ]
    },
    "aliases": {
        "jspd_tta-txntrend": {}
    }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/maximum_metrics' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "5d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/maximum_metrics' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "maximum_metrics"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "maximum_metrics"
      },
      "sort.field": "date",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "kind": {
        "type": "keyword"
      },
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "entity": {
        "type": "keyword"
      },
      "maximum": {
        "type": "float"
      },
      "date": {
        "type": "date",
        "format": "yyyy-MM-dd"
      }
    }
  }
}'
