How to add a plugin to Telegraf? - plugins

Hello I would to know if someone have all ready add a plugin to telegraf for Influxdb.
I have my go code which is working. What do I need next and where to put theses files?
I've found that I need to do something like this:
type ReadFile struct {
//buf []byte
//MemoryBytes int64
//PID int
}
func (s *ReadFile) Description() string {
return "This is a test plugin to read data from a file and send them to influxdb" }
func (s *ReadFile) SampleConfig() string {
return "ok = true # indicate if everything is fine"
}
func Gather(acc plugins.Accumulator) error {
readFile(alarmFile)
acc.Add("alarm", result_of_readFile_here, tags)
}
}
func init() {
plugins.Add("readFile", func() plugins.Plugin { &ReadFile{} })
}
But is this my entire Go plugin or another file in Go to add with my Go program?
And where does the file.conf is store?
[tags]
dc = "alarm"
[agent]
interval = "10s"
# OUTPUTS
[outputs]
[outputs.influxdb]
url = "http://127.0.0.1:8086" # required.
database = "summer" # required.
precision = "s"
# PLUGINS
[readFile]
If you have a list of what I need, how to structure it, where I store file or maybe an example could be really helpful.
Thanks!!

-> I receive this, it gave me a better understanding, I think it could be helpful:
https://github.com/influxdata/telegraf/blob/master/CONTRIBUTING.md
"His plugin code looks good to go. He needs to place that file in $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/testPlugin/testPlugin.go
He should write a test for the plugin and place it at $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/testPlugin/testPlugin_test.go
After this is complete he needs to register the plugin at $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/all/all.go
Then he should run make from $GOPATH/src/github.com/influxdata/telegraf. This will place the new telegraf binary in $GOPATH/bin/telegraf.
Run the binary with the following flags to generate the valid configuration:
$GOPATH/bin/telegraf -sample-config -input-filter testPlugin -output-filter influxdb > testPlugin_config.conf
From there you can run the binary with the -test flag by passing it the sample config:
$GOPATH/bin/telegraf -config testPlugin_config.conf -test
This will output the line protocol that is to be inserted into the database."
-> And the testPlugin.go that he talks about:
package testPlugin
import (
"time"
)
type ReadFile struct {
counter int64
}
func (s *TestPlugin) Description() string {
return "This is a test plugin to write data to influxdb with a plugin"
}
func (s *TestPlugin) SampleConfig() string {
return "ok = true # indicate if everything is fine"
}
func Gather(acc telegraf.Accumulator) error {
c := time.Tick(10 * time.Second)
for now := range c {
counter := counter + 1
acc.Add("counter",counter, tags)
}
}
func init() {
inputs.Add("testPlugin", func() telegraf.Input { return &TestPlugin{} })
}

There's an opened issue for external plugin support which might be part of Telegraf 1.4.0. If will probably load external *.so files.
Until then all plugins are supposed to be merged into master repository via PRs. There are already many plugins waiting in review process. This model is obviously not very scalable.

Related

Why 'link' variable gets changed to null even after i assign it a value

private fun shareOperation(file: File) {
val uri = Uri.fromFile(file)
val storage = FirebaseStorage.getInstance()
val pdfRef = storage.reference.child("pdf/${uri.lastPathSegment}")
pdfRef.putFile(uri).addOnFailureListener { e ->
Log.e(TAG, "Couldn't share " + e.message)
}.addOnCompleteListener{
it.addOnCompleteListener {
pdfRef.downloadUrl.addOnSuccessListener { e ->
run {
link = e.toString()
Log.i(TAG,link!!) // Here i get the link to file in firebase storage
}
}
}
}
// Here link gets null
}
i was expecting somehow i can get the link to the file and can use it for sharing intent
You are performing an asynchronous call to upload the file, that is correct since any UI blocking action must be performed in background. The variable link will be null until the run code is executed in the background thread.
You need to code inside the run block whatever you want to happen when the link is available.
BTW looks weird what you are doing with the nested addOnCompleteListener, there should be an easier way to code that. You should probably spend time learning how to code with listeners and background threads.

dynamically put enpoint url through setup scripts

I have two Environment and a lot of test cases, but not all test cases are needed to be run in all environment. Is there a way to run only an specific test cases from a test suite based on the selected Environment. in Setup script .I am using soap ui version 5.4.0 For Example If I select Environment1, it will run the following test cases TC0001 TC0002 If I select Environment2, it will run only the following test cases TC0003 TC0004
Here as I am using "testcases.each" its replacing all endpoint url to one env but i want to exceute based on the testcasename.I am new to the groovy scripting .So can anyone help on this
I am currently using below code but its replacing all endpoint urls in all test cases::
def result = com.eviware.soapui.support.UISupport.prompt("Please select the enviornment", "Environment", ['UAT_API','IT2','UAT1'])
def testcases = testSuite.getTestCaseList()
if(result == 'UAT1'){
testcases.each { testcase ->
def teststeps = testcase.getTestStepList()
teststeps.each { teststep ->
teststep.setPropertyValue('endpoint','')
}
}
}else if(result == 'UAT_API'){
testcases.each { testcase ->
def teststeps = testcase.getTestStepList()
teststeps.each { teststep ->
teststep.setPropertyValue('endpoint','https://a-api.com')
}
}
}else if(result == 'IT2'){
testcases.each { testcase ->
def teststeps = testcase.getTestStepList()
teststeps.each { teststep ->
teststep.setPropertyValue('endpoint','https://it-webservices.com:1')
}
}
}
Can someone give guidance how to proceed from here to?

Read large file using vertx

I am new to using vertx and I am using vertx filesystem api to read file of large size.
vertx.fileSystem().readFile("target/classes/readme.txt", result -> {
if (result.succeeded()) {
System.out.println(result.result());
} else {
System.err.println("Oh oh ..." + result.cause());
}
});
But the RAM is all consumed while reading and the resource is not even flushed after use. The vertx filesystem api also suggest
Do not use this method to read very large files or you risk running out of available RAM.
Is there any alternative to this?
To read large file you should open an AsyncFile:
OpenOptions options = new OpenOptions();
fileSystem.open("myfile.txt", options, res -> {
if (res.succeeded()) {
AsyncFile file = res.result();
} else {
// Something went wrong!
}
});
Then an AsyncFile is a ReadStream so you can use it together with a Pump to copy the bits to a WriteStream:
Pump.pump(file, output).start();
file.endHandler((r) -> {
System.out.println("Copy done");
});
There are different kind of WriteStream, like AsyncFile, net sockets, HTTP server responses, ...etc.
To read/process a large file in chunks you need to use the open() method which will return an AsyncFile on success. On this AsyncFile you setReadBufferSize() (or not, the default is 8192), and attach a handler() which will be passed a Buffer of at most the size of the read buffer you just set.
In the example below I have also attached an endHandler() to print a final newline to stay in line with the sample code you provided in the question:
vertx.fileSystem().open("target/classes/readme.txt", new OpenOptions().setWrite(false).setCreate(false), result -> {
if (result.succeeded()) {
result.result().setReadBufferSize(READ_BUFFER_SIZE).handler(data -> System.out.print(data.toString()))
.endHandler(v -> System.out.println());
} else {
System.err.println("Oh oh ..." + result.cause());
}
});
You need to define READ_BUFFER_SIZE somewhere of course.
The reason for that is that internally .readFile calls to Files.readAllBytes.
What you should do instead is create a stream out of your file, and pass it to Vertx handler:
try (InputStream steam = new FileInputStream("target/classes/readme.txt")) {
// Your handling here
}

Unlang write to file FreeRADIUS

I know I might be facing an impossible mission. What I do want is radiusd to write down every mac received in an Acces-Request, for later on deny access to those MAC.
I know the policies file is written in unlang, bad news are that radiusd does not have any write permissions on any of the conf files...
Anyway, was anyone capable of WRITTING to a file in the POLICY PROCESSING of FreeRADIUS?
What I want to achieve would be something like this:
raddb/sites-available/default
authorize {
rewrite_calling_station_id
unauthorized_macs
if(ok) {
reject
}
else {
update control {
Auth-Type := Accept
}
GET MAC FROM CALLIN_STATION_ID ATTRIBUTE
WRITE THIS F***ING MAC TO unauthorized_macs FILE
}
}
Thanks to Arran, I could solve this the following way:
authorize {
rewrite_calling_station_id
authMac
if(ok) {
reject
}
else {
linelog
update control {
Auth-Type := Accept
}
}
}
Where linelog is configured as follows:
raddb/mods-enabled/linelog
linelog {
filename = /path/to/hell/authMac
format = "%{Calling-Station-ID}"
}
update request {
Tmp-String-0 := `echo "%{Calling-Station-ID}" >> "/path/to/f___ing_unauthorized_macs_file"`
}
There's also the linelog module which would be better in >= v3.0.x as it implements internal locking (in addition to flock) to prevent line interleaving.
See /etc/raddb/mods-available/linelog for examples.

How to add an import to the file with Babel

Say you have a file with:
AddReactImport();
And the plugin:
export default function ({types: t }) {
return {
visitor: {
CallExpression(p) {
if (p.node.callee.name === "AddReactImport") {
// add import if it's not there
}
}
}
};
}
How do you add import React from 'react'; at the top of the file/tree if it's not there already.
I think more important than the answer is how you find out how to do it. Please tell me because I'm having a hard time finding info sources on how to develop Babel plugins. My sources right now are: Plugin Handbook,Babel Types, AST Spec, this blog post, and the AST explorer. It feels like using an English-German dictionary to try to speak German.
export default function ({types: t }) {
return {
visitor: {
Program(path) {
const identifier = t.identifier('React');
const importDefaultSpecifier = t.importDefaultSpecifier(identifier);
const importDeclaration = t.importDeclaration([importDefaultSpecifier], t.stringLiteral('react'));
path.unshiftContainer('body', importDeclaration);
}
}
};
}
If you want to inject code, just use #babel/template to generate the AST node for it; then inject it as you need to.
Preamble: Babel documentation is not the best
I also agree that, even in 2020, information is sparse. I am getting most of my info by actually working through the babel source code, looking at all the tools (types, traverse, path, code-frame etc...), the helpers they use, existing plugins (e.g. istanbul to learn a bit about basic instrumentation in JS), the webpack babel-loader and more...
For example: unshiftContainer (and actually, babel-traverse in general) has no official documentation, but you can find it's source code here (fascinatingly enough, it accepts either a single node or an array of nodes!)
Strategy #1 (updated version)
In this particular case, I would:
Create a #babel/template
prepare that AST once at the start of my plugin
inject it into Program (i.e. the root path) once, only if the particular function call has been found
NOTE: Templates also support variables. Very useful if you want to wrap existing nodes or want to produce slight variations of the same code, depending on context.
Code (using Strategy #1)
import template from "#babel/template";
// template
const buildImport = template(`
import React from 'react';
`);
// plugin
const plugin = function () {
const importDeclaration = buildImport();
let imported = false;
let root;
return {
visitor: {
Program(path) {
root = path;
},
CallExpression(path) {
if (!imported && path.node.callee.name === "AddMyImport") {
// add import if it's not there
imported = true;
root.unshiftContainer('body', importDeclaration);
}
}
}
};
};
Strategy #2 (old version)
An alternative is:
use a utility function to generate an AST from source (parseSource)
prepare that AST once at the start of my plugin
inject it into Program (i.e. the root path) once, only if the particular function call has been found
Code (using Strategy #2)
Same as above but with your own compiler function (not as efficient as #babel/template):
/**
* Helper: Generate AST from source through `#babel/parser`.
* Copied from somewhere... I think it was `#babel/traverse`
* #param {*} source
*/
export function parseSource(source) {
let ast;
try {
source = `${source}`;
ast = parse(source);
} catch (err) {
const loc = err.loc;
if (loc) {
err.message +=
"\n" +
codeFrameColumns(source, {
start: {
line: loc.line,
column: loc.column + 1,
},
});
}
throw err;
}
const nodes = ast.program.body;
nodes.forEach(n => traverse.removeProperties(n));
return nodes;
}
Possible Pitfalls
When a new node is injected/replaced etc, babel will run all plugins on them again. This is why your first instrumentation plugin is likely to encounter an infinite loop right of the bet: you want to remember and not re-visit previously visited nodes (I'm using a Set for that).
It gets worse when wrapping nodes. Nodes wrapped (e.g. with #babel/template) are actually copies, not the original node. In that case, you want to remember that it is instrumented and skip it in case you come across it again, or, again: infinite loop 💥!
If you don't want to instrument nodes that have been emitted by any plugin (not just yours), that is you want to only operate on the original source code, you can skip them by checking whether they have a loc property (injected nodes usually do not have a loc property).
In your case, you are trying to add an import statement which won't always work without the right plugins enabled or without program-type set to module.
I believe there's an even better way now: babel-helper-module-imports
For you the code would be
import { addDefault } from "#babel/helper-module-imports";
addDefault(path, 'React', { nameHint: "React" })