jiaqi / jmxterm

Interactive command line JMX client
http://docs.cyclopsgroup.org/jmxterm
Apache License 2.0
519 stars 155 forks source link

Accessing environment variables in arguments #118

Open ChrisHuebsch-FLIG opened 7 months ago

ChrisHuebsch-FLIG commented 7 months ago

I have a small suggestion, which would help me a lot in using jmxterm for daily use.

I have a service running with several beans. All those beans expose a jmx-interface.

They support some operation to set a value.

I need to set the value to a subset of those beans.

For this I have a config file, which I execute with the "-i" command switch.

But the config files are static. I need a possibility to "inject" some variable content to the file, because the operation argument is different each time.

Certainly, I could rewrite the file every time with sed/awk/grep or something like that.

What I wish I had was some placeholder method in the command syntax. Like so:

run -b com.xxx.yyy.zzz:name=hello,type=Handler setDateTime "%ddd%"

When the program runs, it should replace "%ddd%" by whatever content is in that specific environment variable named ddd.

I added this for testing to ValueFormat/parseValue, but I am not sure if that is the proper place. Perhaps this could also be part of that jcli library (or it might even already be, but it's not used???)

nyg commented 7 months ago

I don't know if this fits your use case but something like this could be done:

$ MYVAR=somevalue; echo "open 4302\nbean edu.self.nyg.example.jmx.app:Lorem=ipsum,foo=Bar\nrun executeOperation $MYVAR\nclose\nbye" | java -jar target/jmxterm-1.0.4-uber.jar
Apr 05, 2024 10:43:41 AM org.jline.utils.Log logr
WARNING: Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
Delete /Users/user/.jmxterm_history if you encounter error right after launching me.
Welcome to JMX terminal. Type "help" for available commands.
$>open 4302#Connection to 4302 is opened
$>bean edu.self.nyg.example.jmx.app:Lorem=ipsum,foo=Bar#bean is set to edu.self.nyg.example.jmx.app:Lorem=ipsum,foo=Bar
$>run executeOperation somevalue#calling operation executeOperation of mbean edu.self.nyg.example.jmx.app:Lorem=ipsum,foo=Bar with params [somevalue]
#operation returns:
null
$>close#disconnected
$>bye#bye
ChrisHuebsch-FLIG commented 7 months ago

That is certainly a good way to get to the variable content.

But I need to set the same value for several beans. That would require several starts of the jmxterm and a rather complex batch script to do that.

But yes, that would work. An environment expansion would be more convenient, though ;-)

nyg commented 7 months ago

Hmm yes, indeed, that could be a nice feature.

However, as you can see, I'm just another user :). I've made two other PRs but they have not been merged (though approved).