Java – How to add data in openTSDB using java using tCollector

How to add data in openTSDB using java using tCollector… here is a solution to the problem.

How to add data in openTSDB using java using tCollector

My goal is to insert data directly into TSDB via java code, and for this I try to create an executable jar that prints the data to STDOUT. Then I wrote the collector to run this jar and put it in tcollector/collectors/0. Finally, I tried executing the collector in two ways, one using shell scripts and the other using python.

Python collector:

def main():
    while True:
        os.system("java -jar ./TCollectorTSDB.jar")
        sys.stdout.flush()
        time.sleep(COLLECTION_INTERVAL)    
if __name__ == "__main__":
    sys.stdin.close()
    sys.exit(main())

The collector above works just fine, but when I try to run the same jar from a shell script, the collector doesn’t select a shell script.

Shell script collector:

java -jar "./TCollectorTSDB.jar"

Do I need to configure something to register this collector?
If we have to insert data via Java, what’s the best way to do the same with Collector?

Solution

tcollector doesn’t care if the collector you write is in Python or shell script or whatever. As soon as the collector is placed in tcollector/collectors/0, it will pick up automatically. If not, check that your shell script is executable (chmod a+x) and that it starts with shebang (e.g. #!/bin/sh).

Related Problems and Solutions