Jump to content

UsageUpdate function


pfmartin

Recommended Posts

Hi there,

 

I am wondering about the UsageUpdate function that can be called from custom server modules. Is this actually available for developers to use? I have been trying with no success to get this to work. In my server module I have defined a function as described in:

 

http://forum.whmcs.com/showpost.php?p=137763&postcount=7

 

1) You should have already set your cron to run daily.

 

2) In admin -> setup -> Automation Settings down the bottom in Miscellaneous check "Update Usage Statistics"

Quote:

When this is checked the function in your server module is called. In your function you get the stats from your server and then update the stats fields in tblhosting in your DB

For an example if your server module was called freddy.php then the function should look like this

Code:

function freddy_UsageUpdate($params) {

### Your code goes here ###

}

3) If you want whmcs to create the overage invoice at the end of the month then

Quote:

Go to Setup -> Products/Services and edit your product to both enable and set the overage pricing on the other tab.

If you don't want the invoice generated for any users that has this product then don't enable it.

For more info go to http://wiki.whmcs.com/Disk_Space_and...verage_Billing

 

I notice that the official developer documentation makes no mention of this feature. Can someone please confirm that this is available and working? If so, why is it not documented?

 

Thank you.

Link to comment
Share on other sites

Thanks Sparky, that certainly helps.

 

A related question: Is it possible to write messages to the whmcs log so that we can check if something goes wrong during the usageupdate cron call? I cannot find anything describing how to write messages to the log, and that would certainly help so that we can keep track of whether the function is working, and when it fails, we can log the reason it failed.

 

Thanks again for your support!

Link to comment
Share on other sites

Sparky, you are awesome!

 

I hope I can abuse of your generosity a little bit more... I inserted logActivity lines into my module code in the modulename_UsageUpdate() function, and then ran the cron job command by hand. However, there is nothing being logged.

 

Is there any additional step I need to take to ensure that my custom module's UsageUpdate function is run during the cron job call? I am running the admin/cron.php script.

 

Thanks!

Link to comment
Share on other sites

HI Sparky,

 

From the link you sent, I read that the billing calculation of the usage is run once per month. However, the activity log for the cron that I ran by hand shows:

 

Cron Job: Running Usage Stats Update

 

I would think that the usage should update every day in order to let keep track of the most up-to-date usage so far for a customer's account. If it were monthly, it seems that there are chances for things to go wrong if the one-time it is run in the month something doesn't work.

 

As far as I can tell, the UsageUpdate function never gets called by the cron job. I suppose I can wait to see if it ever gets called now that the month of March is ending. I will keep an eye on it and see if my log messages show up.

 

It would however be great if there was a definitive description of how this is supposed to work (that is, from the whmcs developers). I sincerely appreciate your help Sparky, but I still find it strange that whmcs does not have this documented somewhere. The fact that the cron.php code is encrypted does not help.

 

Thanks!

Link to comment
Share on other sites

I was right the first time... the function "YOURMODULENAME_UsageUpdate" is called on every cron run

An example would be (where XXXX is the name of the server module file)

function XXXX_UsageUpdate($params) {
 // call your api to get the usage from the server
 // now update the usage to whmcs tblhosting for all of the hosting accounts for that server.
// generally you would use a foreach statement if the results are returned as an array
}

the params sent to the function is

serverid

serverip

serverusername

serverpassword

serveraccesshash

The fields in tblhosting that should updated are
diskusage

disklimit

bwusage

bwlimit

lastupdate

Only the invoices for bandwidth and disk usage are generated once a month during the cron run.

 

The logActivity() function needs to have the includes/functions.php file to run as that is where the function resides.

During the cron and also when the module runs or is called the functions.php file should already be included so it should work. (unless you have an error in your code)

Edited by sparky
Link to comment
Share on other sites

Hi Sparky,

 

I setup a custom button in the admin so that I could call the XXXX_UpdateUsage($params) function directly. I learned that the ActivityLog does work. It creates log entries just as it should.

 

However, when I run the cron job, it appears that the UpdateUsage function does not get called at all since I do not see the log entries. Therefore, my problem is simply that the cron job is not calling my updateusage function.

 

I have checked all my configurations and it appears to be correct. But clearly, I missed something that makes the cron call my module_UpdateUsage function. I wonder what it can be? I will keep checking.

 

Thanks!

Link to comment
Share on other sites

Have you got admin -> automation settings -> Miscelaneous ->Update Usage Statistics ticked?

(Right at the bottom of the page)

 

Also you need to make sure that the server is setup in admin -> servers

Edited by sparky
Link to comment
Share on other sites

Yes, it is checked. Thanks for asking though! I am kind of going nuts with this. Double checking everything and still the cron does not call the function.

 

The function works beautifully when I call it directly using the custom admin button. It pulls the usage data and updated the database. But without cron...

 

I will keeping poking around, but there has to be something that turns it on somewhere else. I also made sure that the product/service has the overage calculator turned on in the "other" tab. I can't find any other place where there is a setting related to overage billing.

 

Thanks again!

Link to comment
Share on other sites

OK, I finally figured out what I was doing wrong. It was a stupid mistake on my part.

 

The problem was that my function was written assuming that it was per account that the UsageUpdate function was called. Therefore, I expected to have the username in the params so that I could look up the usage for a user.

 

However, the UsageUpdate functions works per server.... This is a different setup that I expected and did not realize that my function was failing before it go to the logActivity function within my code. That is why I was not seeing anything.

 

I want to thank Sparky for all the help. It really was a tremendous effort on your part and I only wish I had noticed this sooner. Also, thanks to Whmcs support who pointed out the fact that it is per server, and not per user that the function is called. This helped crack the case.

 

Thanks!

Link to comment
Share on other sites

  • 3 weeks later...

A question about billing for quota and bandwidth:

 

These are the fields in tblhosting:

 

diskusage

disklimit

bwusage

bwlimit

 

But, there is also the setting for the plan which is:

 

Soft Disk Limit

Soft Bandwidth Limit

 

So, my based on this, what are the disklimit and bwlimit fields for in the tblhosting table?

 

== Here's what I am hoping:

 

We have a hosting plan that allows 1GB of disk space.

 

We offer pricing for increasing your base quota above the 1GB of space.

 

We also offer pricing for when a user goes beyond their quota.

 

So, for example, we may have a user that has the base plan but exceeds their quota in a given month, and therefore we exceed the Soft Limit and charge accordingly.

 

But, we may have a customer on the 1GB plan that pre-pays for an extra 1GB of disk space. Therefore, their Soft Limit is 1GB, but their real limit is 2GB, and anything beyond that is charged at the overage price.

 

== My question is whether this is possible using the settings and table fields above. If so, how do you set this all up?

 

Thanks in advanced for any help!

 

Cheers.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated