<commit_before>// Take well-formed json from a sensu check result and create an elasticsearch document to be used to
// generate user specific dashboards or highly contextual alerts.
//
// LICENSE:
//   Copyright 2015 Yieldbot. <<EMAIL>>
//   Released under the MIT License; see LICENSE
//   for details.

package main

import (
	"flag"
	"fmt"
	"github.com/olivere/elastic"
	"github.com/yieldbot/sensues/lib"
	"github.com/yieldbot/sensuplugin/sensuhandler"
	"github.com/yieldbot/sensuplugin/sensuutil"
	"time"
)

func main() {

	// set commandline flags
	esIndexPtr := flag.String("index", lib.StatusEsIndex, "the elasticsearch index to use")
	esHostPtr := flag.String("host", lib.DefaultEsHost, "the elasticsearch host")
	esPortPtr := flag.String("port", lib.DefaultEsPort, "the elasticsearch port")

	flag.Parse()
	esIndex := *esIndexPtr
	esType := lib.DefaultEsType
	esHost := *esHostPtr
	esPort := *esPortPtr

	sensuEvent := new(sensuhandler.SensuEvent)

	sensuEnv := sensuhandler.SetSensuEnv()
	sensuEvent = sensuEvent.AcquireSensuEvent()

	// Create a client
	client, err := elastic.NewClient(
		elastic.SetURL("http://" + esHost + ":" + esPort),
	)
	if err != nil {
		sensuutil.EHndlr(err)
	}

	// Check to see if the index exists and if not create it
	if client.IndexExists(esIndex) == nil { // need to test to make sure this does what I want
		_, err = client.CreateIndex(esIndex).Do()
		if err != nil {
			sensuutil.EHndlr(err)
		}
	}

	// Create an Elasticsearch document. The document type will define the mapping used for the document.
	doc := make(map[string]interface{})
	var docID string
	docID = sensuhandler.EventName(sensuEvent.Client.Name, sensuEvent.Check.Name)
	doc["monitored_instance"] = sensuEvent.AcquireMonitoredInstance()
	doc["sensu_client"] = sensuEvent.Client.Name
	doc["incident_timestamp"] = time.Unix(sensuEvent.Check.Issued, 0).Format(time.RFC3339)
	doc["check_name"] = sensuhandler.CreateCheckName(sensuEvent.Check.Name)
	doc["check_state"] = sensuhandler.DefineStatus(sensuEvent.Check.Status)
	doc["sensuEnv"] = sensuhandler.DefineSensuEnv(sensuEnv.Sensu.Environment)
	doc["tags"] = sensuEvent.Check.Tags
	doc["instance_address"] = sensuEvent.Client.Address
	doc["check_state_duration"] = sensuhandler.DefineCheckStateDuration()

	// Add a document to the Elasticsearch index
	_, err = client.Index().
		Index(esIndex).
		Type(esType).
		Id(docID).<commit_msg>add debugging statement to print out host info<commit_after>// Take well-formed json from a sensu check result and create an elasticsearch document to be used to
// generate user specific dashboards or highly contextual alerts.
//
// LICENSE:
//   Copyright 2015 Yieldbot. <<EMAIL>>
//   Released under the MIT License; see LICENSE
//   for details.

package main

import (
	"flag"
	"fmt"
	"github.com/olivere/elastic"
	"github.com/yieldbot/sensues/lib"
	"github.com/yieldbot/sensuplugin/sensuhandler"
	"github.com/yieldbot/sensuplugin/sensuutil"
	"time"
)

func main() {

	// set commandline flags
	esIndexPtr := flag.String("index", lib.StatusEsIndex, "the elasticsearch index to use")
	esHostPtr := flag.String("host", lib.DefaultEsHost, "the elasticsearch host")
	esPortPtr := flag.String("port", lib.DefaultEsPort, "the elasticsearch port")

	flag.Parse()
	esIndex := *esIndexPtr
	esType := lib.DefaultEsType
	esHost := *esHostPtr
	esPort := *esPortPtr

	sensuEvent := new(sensuhandler.SensuEvent)

	sensuEnv := sensuhandler.SetSensuEnv()
	sensuEvent = sensuEvent.AcquireSensuEvent()

	// Create a client
	client, err := elastic.NewClient(
		elastic.SetURL("http://" + esHost + ":" + esPort),
	)
	fmt.Printf("http://" = esHost + ":" + esPort)
	if err != nil {
		sensuutil.EHndlr(err)
	}

	// Check to see if the index exists and if not create it
	if client.IndexExists(esIndex) == nil { // need to test to make sure this does what I want
		_, err = client.CreateIndex(esIndex).Do()
		if err != nil {
			sensuutil.EHndlr(err)
		}
	}

	// Create an Elasticsearch document. The document type will define the mapping used for the document.
	doc := make(map[string]interface{})
	var docID string
	docID = sensuhandler.EventName(sensuEvent.Client.Name, sensuEvent.Check.Name)
	doc["monitored_instance"] = sensuEvent.AcquireMonitoredInstance()
	doc["sensu_client"] = sensuEvent.Client.Name
	doc["incident_timestamp"] = time.Unix(sensuEvent.Check.Issued, 0).Format(time.RFC3339)
	doc["check_name"] = sensuhandler.CreateCheckName(sensuEvent.Check.Name)
	doc["check_state"] = sensuhandler.DefineStatus(sensuEvent.Check.Status)
	doc["sensuEnv"] = sensuhandler.DefineSensuEnv(sensuEnv.Sensu.Environment)
	doc["tags"] = sensuEvent.Check.Tags
	doc["instance_address"] = sensuEvent.Client.Address
	doc["check_state_duration"] = sensuhandler.DefineCheckStateDuration()

	// Add a document to the Elasticsearch index
	_, err = client.Index().
		Index(esIndex).
		Type(esType).
		Id(docID).
{'fraction_non_alphanumeric': 0.12761592121460813, 'fraction_numerical': 0.0036930652441526466, 'mean_word_length': 4.675203725261932, 'pattern_counts': {'":': 3, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3737587', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1629, 'n_words': 387}